Erle Robotics Python Networking Gitbook Free

Binding to Interfaces(TCP)

the IP address that you pair with a port number when you perform a bind() operation tells the operating system which network interfaces you are willing to receive connections from. The example invocations of used the localhost IP address, which protects your code from connections originating on other machines. You can verify this by running in server mode as shown previously, and trying to connect with a client from another machine:

[email protected]:~/Python_files# python client
Traceback (most recent call last):
  File "", line 29, in <module>
    s.connect((HOST, PORT))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused

But if you run the server with an empty string for the hostname, which tells the Python bind() routine that you are willing to accept connections through any of your machine’s active network interfaces, then the client can connect successfully from another host:

[email protected]:~/Python_files# python server ""
Listening at ('', 1060)

Run the client:

[email protected]:~/Python_files# python client
Client has been assigned socket name ('', 49696)
The server said 'Farewell, client'

This appear into the server terminal:

We have accepted a connection from ('', 49696)
Socket connects ('', 1060) and ('', 49696)
The incoming sixteen-octet message says 'Hi there, server'
Reply sent, socket closed
Listening at ('', 1060)