Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > UDP sockets

Reply
Thread Tools

UDP sockets

 
 
Zunbeltz Izaola
Guest
Posts: n/a
 
      10-22-2003
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
 
Reply With Quote
 
 
 
 
Andrew Bennetts
Guest
Posts: n/a
 
      10-22-2003
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.


 
Reply With Quote
 
 
 
 
Andrew Bennetts
Guest
Posts: n/a
 
      10-22-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.


 
Reply With Quote
 
Wojtek Walczak
Guest
Posts: n/a
 
      10-22-2003
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." ]

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      10-22-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sockets -- basic udp client 7stud Python 12 02-17-2008 11:01 PM
udp, datagram sockets 7stud Python 6 08-06-2007 10:46 PM
UDP sockets Sven Jacobs C++ 1 05-11-2004 01:21 PM
udp (0) -> udp (0) traffic ? Tom Cisco 2 03-04-2004 06:06 PM
Python and UDP sockets Zunbeltz Izaola Python 3 10-21-2003 01:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57