UDP sockets

Z

Zunbeltz Izaola

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
 
A

Andrew Bennetts

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

Andrew Bennetts

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

Wojtek Walczak

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]
 
P

Peter Hansen

Wojtek said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Do I need a FIFO queue? 8
UDP reading on multiple sockets 5
UDP Multicast Question 2
sockets -- basic udp client 12
udp sockets with python 3
udp sockets 2
Python and UDP sockets 3
Concurrent multicast udp socket 0

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top