Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have written a python script to listen on port 2626 and executed the script.

proof that localhost is listening

tcp        0      0*               LISTEN

I can connect via telnet to this port over localhost with the following command:

:~$ telnet localhost 2626
Trying ::1...
Connected to localhost.
Escape character is '^]'.

However when I try to connect from a remote machine the connection gets refused.

 :~$ telnet 2626
 telnet: Unable to connect to remote host: Connection refused

I even issued the following command to accept traffic on port 2626:

 sudo iptables -A INPUT -p tcp -i eth0 --dport 2626 -j ACCEPT

What could be the reason telnet on port 2626 is refusing connections from remote machines and how can I fix it?

share|improve this question
up vote 6 down vote accepted

The from the netstat output indicates that your python script is only accepting connections on If you wish to be able to accept connections from any address, have it accept connections on

share|improve this answer
thank you! you both had the right answer! award going to first response. – sadmicrowave Jan 11 '12 at 14:07

That output shows that your program is listening on only. Listening sockets (open ports) are either bound to a specific interface, as in this case, or to all interfaces (in which case you'll see as a notation for 'any address'). The implications of binding to only are that only connections through that interface will go to the listening socket, and hence your program. If you're using python's socket library, either bind to your external IP in the bind() call, or bind to all IPs, something like this:

>>> import socket
>>> s = socket.socket()
>>> s.bind(('', 6666))
>>> s.listen(1)

which gives the result you want from netstat

tcp        0      0  *               LISTEN

and indeed can be connected to from another machine

ial@roach> telnet polihale 6666
Connected to polihale.
Escape character is '^]'.
share|improve this answer
Thank you! you both had the right answer! – sadmicrowave Jan 11 '12 at 14:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.