user007
user007

Reputation: 451

How to assign minimum time wait to connect socket

I will send data to server if it is connected. Otherwise it will continue main program. I am using python 2.7. It takes longer time to continue main program if server is not connected.

server=socket.socket()
host="192.168.0.1"
port=12321
try:
    server.connect(host,port)
    server.send('data')
except:
    print"server vot connected"

if the server computer is not power on or the server program is not running. it takes longer time to print server not connected. I want to assign 3 ms to try to connect server if it could not connect it will exit and print server not connected. how to assign the time to wait and get rid of the problem to hang for 1-2 minutes?

any kind of help will be highly appriciated.

Upvotes: 0

Views: 639

Answers (1)

Luc
Luc

Reputation: 1491

The timeout is a property of the socket:

server=socket.socket()
server.settimeout(0.003)
host="192.168.0.1"
port=12321
try:
    server.connect(host,port)
    server.send('data')
except:
    print"server vot connected"

Upvotes: 1

Related Questions