how do I use select to timeout a call to accept?

Jake Speed speed at ?.com
Thu Nov 9 08:46:19 EST 2000


josh at open.com (Joshua Muskovitz) wrote in <3a0a4362_4 at corp.newsfeeds.com>:

>Basically, I want to be able to do something like:
>
>    try:
>        <magic which either accepts a connection or times out>
>        clientsocket, address = <more magic>
>    except <it was a timeout>:
>  

'select' returns a readable status on a listening socket
that is ready to accept.   Here is a short script that 
listens on port 8081 for ten seconds.


import socket, select

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("", 8081))
server.listen(5)

timeout = 10
(rd, wr, ex) = select.select([server], [], [], timeout)

if server in rd:
        (conn, addr) = server.accept()
        print "Connection:", conn, addr
else:
        print "Timeout!"


-Speed!



More information about the Python-list mailing list