Python's SocketServer won''t handshake

553 views Asked by At

The problem

I am trying to use the SocketServer that ships with Python but I have some issues in the handshake phase. The handshake works fine as long as I use localhost or 127.0.0.1. However when I put the IP of my eth0 card it just won't handshake. I test the official example code found here:

import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        self.data = self.request.recv(1024).strip()
        print "{} wrote:".format(self.client_address[0])
        print self.data
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

Terminal snippet:

manos@box:~$ netcat 10.2.203.26 9999
manos@box:~$
manos@box:~$ netcat localhost 9999
test

As you see in the snippet, trying to connect to 10.2.293.26 which is the IP of my network card doesn't work. As soon as I try to connect to localhost, it works fine!

On Wireshark I get a [RST, ACK] after the first SYN being sent from the client to the server (in the initial handshake phase).

Works fine with telnet

My first guess was that this was a router resetting the connection. However that is not the case since I can telnet fine:

Terminal 1:

manos@box:~/tmp/test$ netcat -l 9999
(waiting)
test

Terminal 2:

manos@box:~$ netcat 10.2.203.26 9999
test
1

There are 1 answers

0
Yoel On BEST ANSWER

You're binding to localhost, which doesn't allow specifying the IP address/es of the machine's interface/s as the destination on the connecting host.

You should specify 0.0.0.0 or the empty string as the machine's address in the bind operation (the HOST argument in your example) in order to allow the remote host to specify any of the machine's IP addresses as the destination.

For more information about the difference between localhost, 127.0.0.1 and 0.0.0.0 refer to this answer or to this one.