monero/external/miniupnpc/testupnpigd.py

89 lines
2.4 KiB
Python
Raw Normal View History

2014-04-09 12:14: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
2017-08-30 19:13:02 +00:00
try:
from http.server import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
2014-04-09 12:14:35 +00:00
# function definition
def list_redirections():
i = 0
while True:
p = u.getgenericportmapping(i)
if p==None:
break
2017-08-30 19:13:02 +00:00
print(i, p)
2014-04-09 12:14:35 +00:00
i = i + 1
#define the handler class for HTTP connections
2017-08-30 19:13:02 +00:00
class handler_class(BaseHTTPRequestHandler):
2014-04-09 12:14:35 +00:00
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write("OK MON GARS")
# 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:
2017-08-30 19:13:02 +00:00
print('Discovering... delay=%ums' % u.discoverdelay)
2014-04-09 12:14:35 +00:00
ndevices = u.discover()
2017-08-30 19:13:02 +00:00
print(ndevices, 'device(s) detected')
2014-04-09 12:14:35 +00:00
# select an igd
u.selectigd()
# display information about the IGD and the internet connection
2017-08-30 19:13:02 +00:00
print('local ip address :', u.lanaddr)
2014-04-09 12:14:35 +00:00
externalipaddress = u.externalipaddress()
2017-08-30 19:13:02 +00:00
print('external ip address :', externalipaddress)
print(u.statusinfo(), u.connectiontype())
2014-04-09 12:14:35 +00:00
#instanciate a HTTPd object. The port is assigned by the system.
2017-08-30 19:13:02 +00:00
httpd = HTTPServer((u.lanaddr, 0), handler_class)
2014-04-09 12:14: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')
2017-08-30 19:13:02 +00:00
print('trying to redirect %s port %u TCP => %s port %u TCP' % (externalipaddress, eport, u.lanaddr, httpd.server_port))
2014-04-09 12:14:35 +00:00
b = u.addportmapping(eport, 'TCP', u.lanaddr, httpd.server_port,
'UPnP IGD Tester port %u' % eport, '')
if b:
2017-08-30 19:13:02 +00:00
print('Success. Now waiting for some HTTP request on http://%s:%u' % (externalipaddress ,eport))
2014-04-09 12:14:35 +00:00
try:
httpd.handle_request()
httpd.server_close()
2017-08-30 19:13:02 +00:00
except KeyboardInterrupt as details:
print("CTRL-C exception!", details)
2014-04-09 12:14:35 +00:00
b = u.deleteportmapping(eport, 'TCP')
if b:
2017-08-30 19:13:02 +00:00
print('Successfully deleted port mapping')
2014-04-09 12:14:35 +00:00
else:
2017-08-30 19:13:02 +00:00
print('Failed to remove port mapping')
2014-04-09 12:14:35 +00:00
else:
2017-08-30 19:13:02 +00:00
print('Failed')
2014-04-09 12:14:35 +00:00
httpd.server_close()
2017-08-30 19:13:02 +00:00
except Exception as e:
print('Exception :', e)