Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cant select.select() catch closed socket error?

Tags:

python

sockets

A closed client socket appended into elist, but there is no exception in ex when select called. I don't known why, can you help me please. Thanks so much!

r,w,ex = select.select(rlist, wlist,elist )
    for s in ex:
       print "catch a closed socket error!"
like image 325
nguyenngoc101 Avatar asked Aug 14 '12 09:08

nguyenngoc101


1 Answers

To catch a socket being closed event you need to have the socket in rlist. When a connection is closed on the other side, select returns and denotes that the closed socket is ready for reading (i.e. it's in you r list). If you perform recv on this socket and it returns an empty list (nothing has been read), it means a connection has been closed.

 data = s.recv(size) 
 if not data: 
    # socket has been closed by peer
    s.close()

From what I have seen, the exception list (the last parameter of select) isn't widely used. The main problem with it is that different OSes interpret this parameter differently. Sometimes it's even used only for cases that don't seem to be exceptions at all (OOB data).

socket module documentation isn't specific about what that argument means, so it's generally not a good idea to rely on it:

xlist: wait for an “exceptional condition” (see the manual page for what your system considers such a condition)

like image 70
Maksim Skurydzin Avatar answered Oct 12 '22 07:10

Maksim Skurydzin