[docs] socket programming HOWTO - correction for bind to ''

David Lim lim at alumni.caltech.edu
Mon May 3 08:01:58 CEST 2010


http://docs.python.org/howto/sockets.html


it says:

A couple things to notice: we used socket.gethostname() so that the socket would be visible   to the outside world. If we had used s.bind(('',80)) or s.bind(('localhost', 80)) or s.bind(('127.0.0.1', 80)) we would still have a “server” socket, but one that was only visible within the same machine.

Actually I believe if you do s.bind(('',80)), that corresponds to INADDR_ANY for the host
which means that socket is visible to the outside world. I.e. the socket will respond
to any incoming connection to the host.  Binding it to socket.gethostname()
means it will respond to a specific incoming connection to that hostname. Thus
if you have a multi-homed host (multiple hostnames for the machine), it will respond to
only one specific hostname.

so I would correct the example code from

serversocket.bind((socket.gethostname(), 80))

to

serversocket.bind(('', 80))

and the text to say

A couple things to notice: we used the empty string ('') so that the socket would be visible   to the outside world. If we had used s.bind(('localhost', 80)) or s.bind(('127.0.0.1', 80)) we would still have a “server” socket, but one that was only visible within the same machine.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3835 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/docs/attachments/20100502/e8e83d17/attachment.bin>


More information about the docs mailing list