3

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

proof that localhost is listening

tcp        0      0 127.0.0.1:2626          0.0.0.0:*               LISTEN

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

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

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

 :~$ telnet 128.251.xxx.xxx 2626
 Trying 128.251.xxx.xxx...
 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?

2 Answers 2

7

That output shows that your program is listening on 127.0.0.1 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 0.0.0.0 as a notation for 'any address'). The implications of binding to 127.0.0.1 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 0.0.0.0:6666            0.0.0.0:*               LISTEN

and indeed can be connected to from another machine

ial@roach> telnet polihale 6666
Trying 128.243.20.139...
Connected to polihale.
Escape character is '^]'.
0
6

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

1
  • thank you! you both had the right answer! award going to first response. Jan 11, 2012 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .