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