Making a publicly available TCPServer from behind a router

764 Views Asked by At

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

2

There are 2 best solutions below

0
On

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.

6
On
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)