How to make socket.listen(1) work for some time and then continue rest of code???

Posted by Rami Jarrar on Stack Overflow See other posts from Stack Overflow or by Rami Jarrar
Published on 2010-03-14T22:59:56Z Indexed on 2010/03/14 23:05 UTC
Read the original article Hit count: 352

Filed under:
|
|

I'm making server that make a tcp socket and work over port range, with each port it will listen on that port for some time, then continue the rest of the code.

like this::

import socket

sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

msg =''
ports = [x for x in xrange(4000)]
while True:
    try:
        for i in ports:
            sck.bind(('',i))
            ## sck.listen(1)
            ## make it just for some time and then continue this

            ## if there a connection do this
                conn, addr = sck.accept()
                msg = conn.recv(2048)
                ## do something
            ##if no connection continue the for loop
            conn.close()
    except KeyboardInterrupt:
        exit()

so how i could make sck.listen(1) work just for some time ??

© Stack Overflow or respective owner

Related posts about python

Related posts about sockets