Can't get UDP example to work

Paul Barry pauljbarry3 at
Sun Jul 26 15:40:39 EDT 2009

On Jul 26, 12:53 pm, Roy Smith <r... at> wrote:
> In article
> <148abf0f-c9e4-4156-8f16-e4e5615d3... at>,
>  Paul Barry <pauljbar... 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.

You are right.  The example in the book actually has

host = ''

So the comment is correct.  In trying to debug what was going on, I
put to see if that had something to do with it, which it

> 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.

Yes, that did the trick, thanks.  I should have thought of that, if I
would have just printed something after the recvfrom line I would have
realized it was no longer blocking and the problem was with getting
the client to print the data to stdout, not with getting a response
from the server.

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

I like the book so far.  It's a little verbose, but I think that's
because he's trying to teach the concepts, rather than the write it
how you actually would in production.  In this case, I think he's
trying to illustrate how the UDP example compares to the TCP example
from the previous section, which is why he choose to keep the methods
the same.

But why not use

    print buf[0]

instead of


that much, I don't know.

More information about the Python-list mailing list