UDP sockets

Andrew Bennetts andrew-pythonlist at puzzling.org
Wed Oct 22 08:10:44 EDT 2003


On Wed, Oct 22, 2003 at 10:02:00PM +1000, Andrew Bennetts wrote:
> On Wed, Oct 22, 2003 at 11:15:35AM +0200, Zunbeltz Izaola wrote:
> > Hi
> > 
> > I'm porting a client writen in C++ to python. What is the way to get a
> > timeout in an select for one socket? 
> > the c++ code is:
> > 
> > 
> >     FD_ZERO(&fds);
> >     FD_SET(fd_sock, &fds);
> >     tv.tv_sec=2;
> >     tv.tv_usec=0;
> >     n = select(fd_sock+1,&fds,NULL,NULL,&tv);
> >     sendto(...)
> 
> The python code is (assuming your socket.socket object is in a variable
> called 'sock'):
> 
>     import select
>     r, w, e = select.select([sock], [], [], 2)
>     if r:
>         sock.sendto(...)

Oh, I hurried too much and misread your code.  You seem to want to always
call sendto, regardless of the result of the select call.  In that case it's
even easier:

    import select
    select.select([sock], [], [], 2)  # Just throw away the results :)
    sock.sendto(...)

Anyway, hopefully it should be clear enough how to use select in python now
:)

-Andrew.






More information about the Python-list mailing list