Annotation of embedaddon/miniupnpc/testupnpigd.py, revision 1.1.1.2
1.1.1.2 ! misho 1: #! /usr/bin/env python
! 2: # $Id: testupnpigd.py,v 1.7 2020/04/06 10:23:02 nanard Exp $
1.1 misho 3: # MiniUPnP project
4: # Author : Thomas Bernard
5: # This Sample code is public domain.
1.1.1.2 ! misho 6: # website : https://miniupnp.tuxfamily.org/
1.1 misho 7:
8: # import the python miniupnpc module
9: import miniupnpc
10: import socket
1.1.1.2 ! misho 11:
! 12: try:
! 13: from http.server import BaseHTTPRequestHandler, HTTPServer
! 14: except ImportError:
! 15: from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
1.1 misho 16:
17: # function definition
18: def list_redirections():
19: i = 0
20: while True:
21: p = u.getgenericportmapping(i)
22: if p==None:
23: break
1.1.1.2 ! misho 24: print(i, p)
1.1 misho 25: i = i + 1
26:
27: #define the handler class for HTTP connections
1.1.1.2 ! misho 28: class handler_class(BaseHTTPRequestHandler):
1.1 misho 29: def do_GET(self):
30: self.send_response(200)
31: self.end_headers()
1.1.1.2 ! misho 32: self.wfile.write(b"OK MON GARS")
1.1 misho 33:
34: # create the object
35: u = miniupnpc.UPnP()
36: #print 'inital(default) values :'
37: #print ' discoverdelay', u.discoverdelay
38: #print ' lanaddr', u.lanaddr
39: #print ' multicastif', u.multicastif
40: #print ' minissdpdsocket', u.minissdpdsocket
41: u.discoverdelay = 200;
42:
43: try:
1.1.1.2 ! misho 44: print('Discovering... delay=%ums' % u.discoverdelay)
1.1 misho 45: ndevices = u.discover()
1.1.1.2 ! misho 46: print(ndevices, 'device(s) detected')
1.1 misho 47:
48: # select an igd
49: u.selectigd()
50: # display information about the IGD and the internet connection
1.1.1.2 ! misho 51: print('local ip address :', u.lanaddr)
1.1 misho 52: externalipaddress = u.externalipaddress()
1.1.1.2 ! misho 53: print('external ip address :', externalipaddress)
! 54: print(u.statusinfo(), u.connectiontype())
1.1 misho 55:
56: #instanciate a HTTPd object. The port is assigned by the system.
1.1.1.2 ! misho 57: httpd = HTTPServer((u.lanaddr, 0), handler_class)
1.1 misho 58: eport = httpd.server_port
59:
60: # find a free port for the redirection
61: r = u.getspecificportmapping(eport, 'TCP')
62: while r != None and eport < 65536:
63: eport = eport + 1
64: r = u.getspecificportmapping(eport, 'TCP')
65:
1.1.1.2 ! misho 66: print('trying to redirect %s port %u TCP => %s port %u TCP' % (externalipaddress, eport, u.lanaddr, httpd.server_port))
1.1 misho 67:
68: b = u.addportmapping(eport, 'TCP', u.lanaddr, httpd.server_port,
69: 'UPnP IGD Tester port %u' % eport, '')
70: if b:
1.1.1.2 ! misho 71: print('Success. Now waiting for some HTTP request on http://%s:%u' % (externalipaddress ,eport))
1.1 misho 72: try:
73: httpd.handle_request()
74: httpd.server_close()
1.1.1.2 ! misho 75: except KeyboardInterrupt as details:
! 76: print("CTRL-C exception!", details)
1.1 misho 77: b = u.deleteportmapping(eport, 'TCP')
78: if b:
1.1.1.2 ! misho 79: print('Successfully deleted port mapping')
1.1 misho 80: else:
1.1.1.2 ! misho 81: print('Failed to remove port mapping')
1.1 misho 82: else:
1.1.1.2 ! misho 83: print('Failed')
1.1 misho 84:
85: httpd.server_close()
86:
1.1.1.2 ! misho 87: except Exception as e:
! 88: print('Exception :', e)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>