2

python 2.7.2 windows 7 32 bit

Let me preface this question with I'm fairly new to network programming. I've created a simple server and client using python's builtin SocketServer

SocketServer.ThreadingTCPServer((host, port), Handler)

This works fine when the host is 'localhost' and the client and server are on the same machine. However I want to be able to access the server from across the internet. My computer is behind a router. I'm using pystun(below) to determine my computers public ip address but am unable to connect. I've done a bit of research and it's my understanding that when a program opens a port the router recognizes that and forwards traffic to that computer. This doesn't appear to be happening. I get the same result with my firewall disabled. Do I need to use port forwarding? How are most applications able to communicate from behind a router? I've looked into puncher (source below) and port forwarding via win32com here. I'm looking for someone to point me in the right direction. How do I programmatically have a server open a listening port from behind a router and forward traffic to it?

puncher source: https://coherence.beebits.net/svn/trunk/Puncher/puncher/base.py pystun: http://pypi.python.org/pypi/pystun

icktoofay
  • 126,289
  • 21
  • 250
  • 231
user2682863
  • 3,097
  • 1
  • 24
  • 38
  • possible duplicate of [Python and sockets +upnp](http://stackoverflow.com/questions/717687/python-and-sockets-upnp) – sarnold Jan 23 '12 at 00:38
  • Many, but not all, routers support [UPnP NAT traversal](http://en.wikipedia.org/wiki/Upnp#NAT_traversal). For the ones that do, this can be the solution. For the ones that don't, you can ask the admin politely to port forward for you. :) – sarnold Jan 23 '12 at 00:39
  • I read up on the NAT traversal. Do you have any examples of code using this principle? – user2682863 Jan 23 '12 at 04:26

2 Answers2

1
lafibre.info/download/upnp/igdctl.pl

I've been using this script for ages, it's obviously in perl so you can either use it as is, or port it to python.

FWIW, I've been meaning to port it to python for a while now, so if you want a hand, ping me (richoH on github)

richo
  • 8,717
  • 3
  • 29
  • 47
  • yeah, it'd be great to port this to python. Unfortunately I don't speak perl. I'm having a look at it right now in notepad. – user2682863 Jan 23 '12 at 03:49
  • If someone's actually going to use it it helps motivate me. I'll see if I can find time tonight and ping you here if I get around to it. – richo Jan 23 '12 at 03:56
  • sounds great. I'm trying to pick through it, but its difficult without the line breaks. – user2682863 Jan 23 '12 at 04:24
  • Wow, that code looks _rough_. It also relies upon the CPAN modules [`Net::UPnP::ControlPoint`](http://search.cpan.org/~skonno/Net-UPnP-1.2.1/lib/Net/UPnP/ControlPoint.pm) and [`Net::UPnP::Device`](http://search.cpan.org/~skonno/Net-UPnP-1.2.1/lib/Net/UPnP/Device.pm) to do most of the actual work. I'd look elsewhere. – sarnold Jan 23 '12 at 22:47
  • I just found [Puncher](http://coherence.beebits.net/wiki/Puncher#OpenTicketsnew..%2E), a Python-twisted based similar tool. Skimming the code it looks nicer than the Perl version, but I haven't personally used it. – sarnold Jan 23 '12 at 22:56
  • sarnold: It's not worth reinventing the wheel unless you have to. I'm desperately trying to find a lib to interact with UPnP for my rewrite, although in saying that I'd rather avoid twisted if I can get away with it. – richo Jan 24 '12 at 01:50
0

This (port-forwarding) needs to be setup at the router. Since there are (likely) several devices behind the router, it needs to know which device to forward traffic coming into port 80 (or whatever) to.

d0nut
  • 610
  • 5
  • 5