miniupnp/miniupnpc/pymoduletest.py

92 lines
2.9 KiB
Python
Raw Permalink Normal View History

2020-04-06 10:25:34 +00:00
#! /usr/bin/env python
# vim: tabstop=2 shiftwidth=2 expandtab
2011-09-27 20:25:35 +00:00
# MiniUPnP project
# Author : Thomas Bernard
2021-11-09 18:56:15 +00:00
# Python 3
2011-09-27 20:25:35 +00:00
# This Sample code is public domain.
2018-01-16 01:06:46 +00:00
# website : https://miniupnp.tuxfamily.org/
2011-09-27 20:25:35 +00:00
# import the python miniupnpc module
import miniupnpc
import sys
try:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--multicastif')
parser.add_argument('-p', '--minissdpdsocket')
parser.add_argument('-d', '--discoverdelay', type=int, default=200)
parser.add_argument('-z', '--localport', type=int, default=0)
# create the object
u = miniupnpc.UPnP(**vars(parser.parse_args()))
except:
2021-11-09 18:56:15 +00:00
print('argparse not available')
i = 1
multicastif = None
minissdpdsocket = None
discoverdelay = 200
localport = 0
while i < len(sys.argv):
2021-11-09 18:56:15 +00:00
print(sys.argv[i])
if sys.argv[i] == '-m' or sys.argv[i] == '--multicastif':
multicastif = sys.argv[i+1]
elif sys.argv[i] == '-p' or sys.argv[i] == '--minissdpdsocket':
minissdpdsocket = sys.argv[i+1]
elif sys.argv[i] == '-d' or sys.argv[i] == '--discoverdelay':
discoverdelay = int(sys.argv[i+1])
elif sys.argv[i] == '-z' or sys.argv[i] == '--localport':
localport = int(sys.argv[i+1])
else:
raise Exception('invalid argument %s' % sys.argv[i])
i += 2
# create the object
u = miniupnpc.UPnP(multicastif, minissdpdsocket, discoverdelay, localport)
2021-11-09 18:56:15 +00:00
print('inital(default) values :')
print(' discoverdelay', u.discoverdelay)
print(' lanaddr', u.lanaddr)
print(' multicastif', u.multicastif)
print(' minissdpdsocket', u.minissdpdsocket)
2011-09-27 20:25:35 +00:00
#u.minissdpdsocket = '../minissdpd/minissdpd.sock'
2018-01-09 01:32:39 +00:00
# discovery process, it usually takes several seconds (2 seconds or more)
2021-11-09 18:56:15 +00:00
print('Discovering... delay=%ums' % u.discoverdelay)
print('%d device(s) detected' % u.discover())
2011-09-27 20:25:35 +00:00
# select an igd
try:
u.selectigd()
2021-11-09 18:56:15 +00:00
except Exception as e:
print('Exception :', e)
2011-09-27 20:25:35 +00:00
sys.exit(1)
2021-11-09 18:56:15 +00:00
# it is also possible to pass the root description URL to u.selectigd() :
# u.selectigd('http://192.168.1.254:5678/desc/root')
2011-09-27 20:25:35 +00:00
# display information about the IGD and the internet connection
2021-11-09 18:56:15 +00:00
print('local ip address :', u.lanaddr)
print('external ip address :', u.externalipaddress())
print( u.statusinfo(), u.connectiontype())
print('total bytes : sent', u.totalbytesent(), 'received', u.totalbytereceived())
print('total packets : sent', u.totalpacketsent(), 'received', u.totalpacketreceived())
2011-09-27 20:25:35 +00:00
#print u.addportmapping(64000, 'TCP',
# '192.168.1.166', 63000, 'port mapping test', '')
#print u.deleteportmapping(64000, 'TCP')
port = 0
proto = 'UDP'
# list the redirections :
i = 0
while True:
p = u.getgenericportmapping(i)
if p==None:
break
2021-11-09 18:56:15 +00:00
print(i, p)
2011-09-27 20:25:35 +00:00
(port, proto, (ihost,iport), desc, c, d, e) = p
#print port, desc
i = i + 1
2021-11-09 18:56:15 +00:00
print(u.getspecificportmapping(port, proto))
try:
2021-11-09 18:56:15 +00:00
print(u.getportmappingnumberofentries())
except Exception as e:
print('GetPortMappingNumberOfEntries() is not supported :', e)
2011-09-27 20:25:35 +00:00