Annotation of embedaddon/miniupnpd/miniupnpc/testupnpigd.py, revision 1.1.1.1

1.1       misho       1: #! /usr/bin/env python
                      2: # $Id: testupnpigd.py,v 1.7 2020/04/06 10:23:02 nanard Exp $
                      3: # MiniUPnP project
                      4: # Author : Thomas Bernard
                      5: # This Sample code is public domain.
                      6: # website : https://miniupnp.tuxfamily.org/
                      7: 
                      8: # import the python miniupnpc module
                      9: import miniupnpc
                     10: import socket
                     11: 
                     12: try:
                     13:     from http.server import BaseHTTPRequestHandler, HTTPServer
                     14: except ImportError:
                     15:     from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
                     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
                     24:                print(i, p)
                     25:                i = i + 1
                     26: 
                     27: #define the handler class for HTTP connections
                     28: class handler_class(BaseHTTPRequestHandler):
                     29:        def do_GET(self):
                     30:                self.send_response(200)
                     31:                self.end_headers()
                     32:                self.wfile.write(b"OK MON GARS")
                     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:
                     44:        print('Discovering... delay=%ums' % u.discoverdelay)
                     45:        ndevices = u.discover()
                     46:        print(ndevices, 'device(s) detected')
                     47: 
                     48:        # select an igd
                     49:        u.selectigd()
                     50:        # display information about the IGD and the internet connection
                     51:        print('local ip address :', u.lanaddr)
                     52:        externalipaddress = u.externalipaddress()
                     53:        print('external ip address :', externalipaddress)
                     54:        print(u.statusinfo(), u.connectiontype())
                     55: 
                     56:        #instanciate a HTTPd object. The port is assigned by the system.
                     57:        httpd = HTTPServer((u.lanaddr, 0), handler_class)
                     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: 
                     66:        print('trying to redirect %s port %u TCP => %s port %u TCP' % (externalipaddress, eport, u.lanaddr, httpd.server_port))
                     67: 
                     68:        b = u.addportmapping(eport, 'TCP', u.lanaddr, httpd.server_port,
                     69:                            'UPnP IGD Tester port %u' % eport, '')
                     70:        if b:
                     71:                print('Success. Now waiting for some HTTP request on http://%s:%u' % (externalipaddress ,eport))
                     72:                try:
                     73:                        httpd.handle_request()
                     74:                        httpd.server_close()
                     75:                except KeyboardInterrupt as details:
                     76:                        print("CTRL-C exception!", details)
                     77:                b = u.deleteportmapping(eport, 'TCP')
                     78:                if b:
                     79:                        print('Successfully deleted port mapping')
                     80:                else:
                     81:                        print('Failed to remove port mapping')
                     82:        else:
                     83:                print('Failed')
                     84: 
                     85:        httpd.server_close()
                     86: 
                     87: except Exception as e:
                     88:        print('Exception :', e)

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>