mirror of
https://github.com/status-im/miniupnp.git
synced 2025-01-19 02:41:25 +00:00
Add python 3 compatibility to IGD test
This commit is contained in:
parent
1579f4a2af
commit
7db2ae5cdd
@ -8,7 +8,11 @@
|
|||||||
# import the python miniupnpc module
|
# import the python miniupnpc module
|
||||||
import miniupnpc
|
import miniupnpc
|
||||||
import socket
|
import socket
|
||||||
import BaseHTTPServer
|
|
||||||
|
try:
|
||||||
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
|
except ImportError:
|
||||||
|
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||||
|
|
||||||
# function definition
|
# function definition
|
||||||
def list_redirections():
|
def list_redirections():
|
||||||
@ -17,11 +21,11 @@ def list_redirections():
|
|||||||
p = u.getgenericportmapping(i)
|
p = u.getgenericportmapping(i)
|
||||||
if p==None:
|
if p==None:
|
||||||
break
|
break
|
||||||
print i, p
|
print(i, p)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
#define the handler class for HTTP connections
|
#define the handler class for HTTP connections
|
||||||
class handler_class(BaseHTTPServer.BaseHTTPRequestHandler):
|
class handler_class(BaseHTTPRequestHandler):
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
@ -37,20 +41,20 @@ u = miniupnpc.UPnP()
|
|||||||
u.discoverdelay = 200;
|
u.discoverdelay = 200;
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print 'Discovering... delay=%ums' % u.discoverdelay
|
print('Discovering... delay=%ums' % u.discoverdelay)
|
||||||
ndevices = u.discover()
|
ndevices = u.discover()
|
||||||
print ndevices, 'device(s) detected'
|
print(ndevices, 'device(s) detected')
|
||||||
|
|
||||||
# select an igd
|
# select an igd
|
||||||
u.selectigd()
|
u.selectigd()
|
||||||
# display information about the IGD and the internet connection
|
# display information about the IGD and the internet connection
|
||||||
print 'local ip address :', u.lanaddr
|
print('local ip address :', u.lanaddr)
|
||||||
externalipaddress = u.externalipaddress()
|
externalipaddress = u.externalipaddress()
|
||||||
print 'external ip address :', externalipaddress
|
print('external ip address :', externalipaddress)
|
||||||
print u.statusinfo(), u.connectiontype()
|
print(u.statusinfo(), u.connectiontype())
|
||||||
|
|
||||||
#instanciate a HTTPd object. The port is assigned by the system.
|
#instanciate a HTTPd object. The port is assigned by the system.
|
||||||
httpd = BaseHTTPServer.HTTPServer((u.lanaddr, 0), handler_class)
|
httpd = HTTPServer((u.lanaddr, 0), handler_class)
|
||||||
eport = httpd.server_port
|
eport = httpd.server_port
|
||||||
|
|
||||||
# find a free port for the redirection
|
# find a free port for the redirection
|
||||||
@ -59,26 +63,26 @@ try:
|
|||||||
eport = eport + 1
|
eport = eport + 1
|
||||||
r = u.getspecificportmapping(eport, 'TCP')
|
r = u.getspecificportmapping(eport, 'TCP')
|
||||||
|
|
||||||
print 'trying to redirect %s port %u TCP => %s port %u TCP' % (externalipaddress, eport, u.lanaddr, httpd.server_port)
|
print('trying to redirect %s port %u TCP => %s port %u TCP' % (externalipaddress, eport, u.lanaddr, httpd.server_port))
|
||||||
|
|
||||||
b = u.addportmapping(eport, 'TCP', u.lanaddr, httpd.server_port,
|
b = u.addportmapping(eport, 'TCP', u.lanaddr, httpd.server_port,
|
||||||
'UPnP IGD Tester port %u' % eport, '')
|
'UPnP IGD Tester port %u' % eport, '')
|
||||||
if b:
|
if b:
|
||||||
print 'Success. Now waiting for some HTTP request on http://%s:%u' % (externalipaddress ,eport)
|
print('Success. Now waiting for some HTTP request on http://%s:%u' % (externalipaddress ,eport))
|
||||||
try:
|
try:
|
||||||
httpd.handle_request()
|
httpd.handle_request()
|
||||||
httpd.server_close()
|
httpd.server_close()
|
||||||
except KeyboardInterrupt, details:
|
except KeyboardInterrupt as details:
|
||||||
print "CTRL-C exception!", details
|
print("CTRL-C exception!", details)
|
||||||
b = u.deleteportmapping(eport, 'TCP')
|
b = u.deleteportmapping(eport, 'TCP')
|
||||||
if b:
|
if b:
|
||||||
print 'Successfully deleted port mapping'
|
print('Successfully deleted port mapping')
|
||||||
else:
|
else:
|
||||||
print 'Failed to remove port mapping'
|
print('Failed to remove port mapping')
|
||||||
else:
|
else:
|
||||||
print 'Failed'
|
print('Failed')
|
||||||
|
|
||||||
httpd.server_close()
|
httpd.server_close()
|
||||||
|
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
print 'Exception :', e
|
print('Exception :', e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user