SRB2/libs/miniupnpc/testupnpigd.py

89 lines
2.4 KiB
Python
Raw Normal View History

2023-12-30 16:35:35 +00:00
#! /usr/bin/env python
# $Id: testupnpigd.py,v 1.7 2020/04/06 10:23:02 nanard Exp $
2014-03-15 16:59:03 +00:00
# MiniUPnP project
# Author : Thomas Bernard
# This Sample code is public domain.
2023-12-30 16:35:35 +00:00
# website : https://miniupnp.tuxfamily.org/
2014-03-15 16:59:03 +00:00
# import the python miniupnpc module
import miniupnpc
import socket
2023-12-30 16:35:35 +00:00
try:
from http.server import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
2014-03-15 16:59:03 +00:00
# function definition
def list_redirections():
i = 0
while True:
p = u.getgenericportmapping(i)
if p==None:
break
2023-12-30 16:35:35 +00:00
print(i, p)
2014-03-15 16:59:03 +00:00
i = i + 1
#define the handler class for HTTP connections
2023-12-30 16:35:35 +00:00
class handler_class(BaseHTTPRequestHandler):
2014-03-15 16:59:03 +00:00
def do_GET(self):
self.send_response(200)
self.end_headers()
2023-12-30 16:35:35 +00:00
self.wfile.write(b"OK MON GARS")
2014-03-15 16:59:03 +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:
2023-12-30 16:35:35 +00:00
print('Discovering... delay=%ums' % u.discoverdelay)
2014-03-15 16:59:03 +00:00
ndevices = u.discover()
2023-12-30 16:35:35 +00:00
print(ndevices, 'device(s) detected')
2014-03-15 16:59:03 +00:00
# select an igd
u.selectigd()
# display information about the IGD and the internet connection
2023-12-30 16:35:35 +00:00
print('local ip address :', u.lanaddr)
2014-03-15 16:59:03 +00:00
externalipaddress = u.externalipaddress()
2023-12-30 16:35:35 +00:00
print('external ip address :', externalipaddress)
print(u.statusinfo(), u.connectiontype())
2014-03-15 16:59:03 +00:00
#instanciate a HTTPd object. The port is assigned by the system.
2023-12-30 16:35:35 +00:00
httpd = HTTPServer((u.lanaddr, 0), handler_class)
2014-03-15 16:59:03 +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')
2023-12-30 16:35:35 +00:00
print('trying to redirect %s port %u TCP => %s port %u TCP' % (externalipaddress, eport, u.lanaddr, httpd.server_port))
2014-03-15 16:59:03 +00:00
b = u.addportmapping(eport, 'TCP', u.lanaddr, httpd.server_port,
'UPnP IGD Tester port %u' % eport, '')
if b:
2023-12-30 16:35:35 +00:00
print('Success. Now waiting for some HTTP request on http://%s:%u' % (externalipaddress ,eport))
2014-03-15 16:59:03 +00:00
try:
httpd.handle_request()
httpd.server_close()
2023-12-30 16:35:35 +00:00
except KeyboardInterrupt as details:
print("CTRL-C exception!", details)
2014-03-15 16:59:03 +00:00
b = u.deleteportmapping(eport, 'TCP')
if b:
2023-12-30 16:35:35 +00:00
print('Successfully deleted port mapping')
2014-03-15 16:59:03 +00:00
else:
2023-12-30 16:35:35 +00:00
print('Failed to remove port mapping')
2014-03-15 16:59:03 +00:00
else:
2023-12-30 16:35:35 +00:00
print('Failed')
2014-03-15 16:59:03 +00:00
httpd.server_close()
2023-12-30 16:35:35 +00:00
except Exception as e:
print('Exception :', e)