miniupnp/miniupnpc/testupnpigd.py

89 lines
2.4 KiB
Python
Raw Normal View History

2011-09-27 20:25:35 +00:00
#! /usr/bin/python
# $Id: testupnpigd.py,v 1.4 2008/10/11 10:27:20 nanard Exp $
# MiniUPnP project
# Author : Thomas Bernard
# This Sample code is public domain.
# website : http://miniupnp.tuxfamily.org/
# import the python miniupnpc module
import miniupnpc
import socket
2016-10-11 19:14:04 +00:00
try:
from http.server import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
2011-09-27 20:25:35 +00:00
# function definition
def list_redirections():
i = 0
while True:
p = u.getgenericportmapping(i)
if p==None:
break
2016-10-11 19:14:04 +00:00
print(i, p)
2011-09-27 20:25:35 +00:00
i = i + 1
#define the handler class for HTTP connections
2016-10-11 19:14:04 +00:00
class handler_class(BaseHTTPRequestHandler):
2011-09-27 20:25:35 +00:00
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b"OK MON GARS")
2011-09-27 20:25:35 +00:00
# create the object
u = miniupnpc.UPnP()
#print 'inital(default) values :'
#print ' discoverdelay', u.discoverdelay
#print ' lanaddr', u.lanaddr
#print ' multicastif', u.multicastif
#print ' minissdpdsocket', u.minissdpdsocket
u.discoverdelay = 200;
try:
2016-10-11 19:14:04 +00:00
print('Discovering... delay=%ums' % u.discoverdelay)
2011-09-27 20:25:35 +00:00
ndevices = u.discover()
2016-10-11 19:14:04 +00:00
print(ndevices, 'device(s) detected')
2011-09-27 20:25:35 +00:00
# select an igd
u.selectigd()
# display information about the IGD and the internet connection
2016-10-11 19:14:04 +00:00
print('local ip address :', u.lanaddr)
2011-09-27 20:25:35 +00:00
externalipaddress = u.externalipaddress()
2016-10-11 19:14:04 +00:00
print('external ip address :', externalipaddress)
print(u.statusinfo(), u.connectiontype())
2011-09-27 20:25:35 +00:00
#instanciate a HTTPd object. The port is assigned by the system.
2016-10-11 19:14:04 +00:00
httpd = HTTPServer((u.lanaddr, 0), handler_class)
2011-09-27 20:25:35 +00:00
eport = httpd.server_port
# find a free port for the redirection
r = u.getspecificportmapping(eport, 'TCP')
while r != None and eport < 65536:
eport = eport + 1
r = u.getspecificportmapping(eport, 'TCP')
2016-10-11 19:14:04 +00:00
print('trying to redirect %s port %u TCP => %s port %u TCP' % (externalipaddress, eport, u.lanaddr, httpd.server_port))
2011-09-27 20:25:35 +00:00
b = u.addportmapping(eport, 'TCP', u.lanaddr, httpd.server_port,
'UPnP IGD Tester port %u' % eport, '')
if b:
2016-10-11 19:14:04 +00:00
print('Success. Now waiting for some HTTP request on http://%s:%u' % (externalipaddress ,eport))
2011-09-27 20:25:35 +00:00
try:
httpd.handle_request()
httpd.server_close()
2016-10-11 19:14:04 +00:00
except KeyboardInterrupt as details:
print("CTRL-C exception!", details)
2011-09-27 20:25:35 +00:00
b = u.deleteportmapping(eport, 'TCP')
if b:
2016-10-11 19:14:04 +00:00
print('Successfully deleted port mapping')
2011-09-27 20:25:35 +00:00
else:
2016-10-11 19:14:04 +00:00
print('Failed to remove port mapping')
2011-09-27 20:25:35 +00:00
else:
2016-10-11 19:14:04 +00:00
print('Failed')
2011-09-27 20:25:35 +00:00
httpd.server_close()
2016-10-11 19:14:04 +00:00
except Exception as e:
print('Exception :', e)