UDP sockets

Discussion in 'Python' started by Zunbeltz Izaola, Oct 22, 2003.

  1. 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(...)

    Thanks

    Zunbeltz

    --
    Remove XXX from email: X
     
    Zunbeltz Izaola, Oct 22, 2003
    #1
    1. Advertising

  2. 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(...)

    -Andrew.
     
    Andrew Bennetts, Oct 22, 2003
    #2
    1. Advertising

  3. 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.
     
    Andrew Bennetts, Oct 22, 2003
    #3
  4. Dnia 22 Oct 2003 11:15:35 +0200, Zunbeltz Izaola napisa³(a):

    > I'm porting a client writen in C++ to python. What is the way to get a
    > timeout in an select for one socket?


    Quote from the ,,7.3 select -- Waiting for I/O completion'':

    The optional timeout argument specifies a time-out as a floating point
    number in seconds.

    So, it'll be something like this:

    n = select.select([fds], [], [], 2)[0][0]

    --
    [ Wojtek Walczak - gminick (at) underground.org.pl ]
    [ <http://gminick.linuxsecurity.pl/> ]
    [ "...rozmaite zwroty, matowe od patyny dawnosci." ]
     
    Wojtek Walczak, Oct 22, 2003
    #4
  5. Zunbeltz Izaola

    Peter Hansen Guest

    Wojtek Walczak wrote:
    >
    > Dnia 22 Oct 2003 11:15:35 +0200, Zunbeltz Izaola napisa³(a):
    >
    > > I'm porting a client writen in C++ to python. What is the way to get a
    > > timeout in an select for one socket?

    >
    > Quote from the ,,7.3 select -- Waiting for I/O completion'':
    >
    > The optional timeout argument specifies a time-out as a floating point
    > number in seconds.
    >
    > So, it'll be something like this:
    >
    > n = select.select([fds], [], [], 2)[0][0]


    At first glance, this appears unsafe. What if the fds socket
    is not ready after the timeout?

    You will, I think, get an IndexError raised.

    -Peter
     
    Peter Hansen, Oct 22, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sven Jacobs

    UDP sockets

    Sven Jacobs, May 9, 2004, in forum: C++
    Replies:
    1
    Views:
    2,020
    Christopher Benson-Manica
    May 11, 2004
  2. Zunbeltz Izaola

    Python and UDP sockets

    Zunbeltz Izaola, Oct 21, 2003, in forum: Python
    Replies:
    3
    Views:
    458
    Zunbeltz Izaola
    Oct 21, 2003
  3. 7stud

    udp, datagram sockets

    7stud, Aug 6, 2007, in forum: Python
    Replies:
    6
    Views:
    466
    7stud
    Aug 6, 2007
  4. 7stud

    sockets -- basic udp client

    7stud, Feb 15, 2008, in forum: Python
    Replies:
    12
    Views:
    753
    7stud
    Feb 17, 2008
  5. inorlando
    Replies:
    3
    Views:
    1,301
    castironpi
    Aug 29, 2008
Loading...

Share This Page