Can't get UDP example to work

Roy Smith roy at
Sun Jul 26 12:53:59 EDT 2009

In article 
<148abf0f-c9e4-4156-8f16-e4e5615d350b at>,
 Paul Barry <pauljbarry3 at> wrote:

> host = ''           # Bind to all interfaces

This threw me off the track for a little while.  The comment is wrong!  
You're not binding to all interfaces, you're binding specifically to the 
loopback interface.  For what you're doing, it's fine, but the comment is 
still wrong.

It's also a little weird that the example uses connect() and sendall().  
Those are TCP-ish type constructs.  They work with UDP sockets, but using 
them sort of obscures the fundamental datagram nature of UDP.

Well, enough of that.  The real problem seems to be right here:

> print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
> while 1:
>     buf = s.recvfrom(2048)
>     sys.stdout.write(buf[0])

Try adding sys.stdout.flush(), and it should work.  Or change it to print.

I've never seen this book, but if this code really is verbatim from the 
book, I'm not very impressed.

More information about the Python-list mailing list