Annotation of embedaddon/miniupnpc/pymoduletest.py, revision 1.1.1.2

1.1.1.2 ! misho       1: #! /usr/bin/env python
        !             2: # vim: tabstop=2 shiftwidth=2 expandtab
1.1       misho       3: # MiniUPnP project
                      4: # Author : Thomas Bernard
1.1.1.2 ! misho       5: # Python 3
1.1       misho       6: # This Sample code is public domain.
1.1.1.2 ! misho       7: # website : https://miniupnp.tuxfamily.org/
1.1       misho       8: 
                      9: # import the python miniupnpc module
                     10: import miniupnpc
                     11: import sys
                     12: 
1.1.1.2 ! misho      13: try:
        !            14:   import argparse
        !            15:   parser = argparse.ArgumentParser()
        !            16:   parser.add_argument('-m', '--multicastif')
        !            17:   parser.add_argument('-p', '--minissdpdsocket')
        !            18:   parser.add_argument('-d', '--discoverdelay', type=int, default=200)
        !            19:   parser.add_argument('-z', '--localport', type=int, default=0)
        !            20:   # create the object
        !            21:   u = miniupnpc.UPnP(**vars(parser.parse_args()))
        !            22: except:
        !            23:   print('argparse not available')
        !            24:   i = 1
        !            25:   multicastif = None
        !            26:   minissdpdsocket = None
        !            27:   discoverdelay = 200
        !            28:   localport = 0
        !            29:   while i < len(sys.argv):
        !            30:     print(sys.argv[i])
        !            31:     if sys.argv[i] == '-m' or sys.argv[i] == '--multicastif':
        !            32:       multicastif = sys.argv[i+1]
        !            33:     elif sys.argv[i] == '-p' or sys.argv[i] == '--minissdpdsocket':
        !            34:       minissdpdsocket = sys.argv[i+1]
        !            35:     elif sys.argv[i] == '-d' or sys.argv[i] == '--discoverdelay':
        !            36:       discoverdelay = int(sys.argv[i+1])
        !            37:     elif sys.argv[i] == '-z' or sys.argv[i] == '--localport':
        !            38:       localport = int(sys.argv[i+1])
        !            39:     else:
        !            40:       raise Exception('invalid argument %s' % sys.argv[i])
        !            41:     i += 2
        !            42:   # create the object
        !            43:   u = miniupnpc.UPnP(multicastif, minissdpdsocket, discoverdelay, localport)
        !            44: 
        !            45: print('inital(default) values :')
        !            46: print(' discoverdelay', u.discoverdelay)
        !            47: print(' lanaddr', u.lanaddr)
        !            48: print(' multicastif', u.multicastif)
        !            49: print(' minissdpdsocket', u.minissdpdsocket)
1.1       misho      50: #u.minissdpdsocket = '../minissdpd/minissdpd.sock'
1.1.1.2 ! misho      51: # discovery process, it usually takes several seconds (2 seconds or more)
        !            52: print('Discovering... delay=%ums' % u.discoverdelay)
        !            53: print('%d device(s) detected' % u.discover())
1.1       misho      54: # select an igd
                     55: try:
                     56:   u.selectigd()
1.1.1.2 ! misho      57: except Exception as e:
        !            58:   print('Exception :', e)
1.1       misho      59:   sys.exit(1)
1.1.1.2 ! misho      60: # it is also possible to pass the root description URL to u.selectigd() :
        !            61: # u.selectigd('http://192.168.1.254:5678/desc/root')
1.1       misho      62: # display information about the IGD and the internet connection
1.1.1.2 ! misho      63: print('local ip address :', u.lanaddr)
        !            64: print('external ip address :', u.externalipaddress())
        !            65: print( u.statusinfo(), u.connectiontype())
        !            66: print('total bytes : sent', u.totalbytesent(), 'received', u.totalbytereceived())
        !            67: print('total packets : sent', u.totalpacketsent(), 'received', u.totalpacketreceived())
1.1       misho      68: 
                     69: #print u.addportmapping(64000, 'TCP',
                     70: #                       '192.168.1.166', 63000, 'port mapping test', '')
                     71: #print u.deleteportmapping(64000, 'TCP')
                     72: 
                     73: port = 0
                     74: proto = 'UDP'
                     75: # list the redirections :
                     76: i = 0
                     77: while True:
                     78:  p = u.getgenericportmapping(i)
                     79:  if p==None:
                     80:    break
1.1.1.2 ! misho      81:  print(i, p)
1.1       misho      82:  (port, proto, (ihost,iport), desc, c, d, e) = p
                     83:  #print port, desc
                     84:  i = i + 1
                     85: 
1.1.1.2 ! misho      86: print(u.getspecificportmapping(port, proto))
        !            87: try:
        !            88:   print(u.getportmappingnumberofentries())
        !            89: except Exception as e:
        !            90:   print('GetPortMappingNumberOfEntries() is not supported :', e)
1.1       misho      91: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>