newbie udp socket question

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Sun May 11 05:38:40 EDT 2003


On Sun, 11 May 2003 00:13:25 +0000, p@ wrote:

> Hi list,
> 
> I have made the following UDP client:
> 
> class UdpClientSocket:
>     def __init__(self,address,port,request):
>         sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>         sock.sendto(str(request),address)
>         response = sock.recv(port) #response here
>         command = response[0]
>         if command =='\x02':
>             print 'server is still alive'
>         if command =='\x04':
>             print 'server tcp queue accepted'
>         if command =='\x06':
>             print 'Member address recieved'
>         if command =='\x08':
>             print 'cluster update redirected'
>         
>         sock.close()
> 
> udpc = UdpClientSocket(('localhost',serverPort),clientPort,'\x01')
> 
> 
> The problem is if I don't get any response from the server it will block
> forever. So how do I implement a timeout for this?
> I have found a tcp-timeout-socket but none for udp...
> 

Using the select module. Replace your receive statement (not tested) :

        ins, outs, errs = select.select( [ sock ] , [], [], 1.0)
        if ins:
            response = sock.recv(port)

-- 
Pedro




More information about the Python-list mailing list