How to set timeout on python socket recv method?

I need to set a timeout on the python socket recv method. How to do it?

+98
python timeout sockets
Apr 27 '10 at 5:51 on
source share
8 answers

A typical approach is to use select () until the data is available or until a timeout occurs. Call recv() only when data is actually available. To be safe, we also set the socket to non-blocking mode to ensure that recv() will never block indefinitely. select() can also be used to wait for more than one socket at a time.

 import select mysocket.setblocking(0) ready = select.select([mysocket], [], [], timeout_in_seconds) if ready[0]: data = mysocket.recv(4096) 

If you have many open file descriptors, poll () is a more efficient alternative to select() .

Another option is to set a timeout for all operations on the socket using socket.settimeout() , but I see that you explicitly rejected this solution in a different answer.

+114
Apr 27 '10 at 13:49 on
source share
+50
Apr 27 '10 at 5:56
source share

As already mentioned, select.select() and socket.settimeout() will work.

Note that you may need to call settimeout twice for your needs, for example.

 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # accept can throw socket.timeout sock.settimeout(5.0) conn, addr = sock.accept() # recv can throw socket.timeout conn.settimeout(5.0) conn.recv(1024) 
+26
Aug 27 '14 at 17:15
source share

You can set the wait time before receiving a response and after receiving a response set it to No:

 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5.0) data = sock.recv(1024) sock.settimeout(None) 
+11
Apr 08 '15 at 8:32
source share

The timeout you are looking for is the connection socket timeout, not the main socket if you are implementing the server side. In other words, there is another timeout for the connection socket object, which is the result of socket.accept() . Hence:

 sock.listen(1) connection, client_address = sock.accept() connection.settimeout(5) # This is the one that affects recv() method. connection.gettimeout() # This should result 5 sock.gettimeout() # This outputs None when not set previously, if I remember correctly. 

If you implement the client side, it will be simple.

 sock.connect(server_address) sock.settimeout(3) 
+4
May 23 '18 at 17:36
source share

As mentioned in previous answers, you can use something like: .settimeout() For example:

 import socket s = socket.socket() s.settimeout(1) # Sets the socket to timeout after 1 second of no activity host, port = "somehost", 4444 s.connect((host, port)) s.send("Hello World!\r\n") try: rec = s.recv(100) # try to receive 100 bytes except socket.timeout: # fail after 1 second of no activity print("Didn't receive data! [Timeout]") finally: s.close() 

Hope this helps !!

+2
Nov 30 '18 at 15:13
source share

You can use socket.settimeout() which takes an integer argument representing the number of seconds. For example, socket.settimeout(1) set the timeout to 1 second

+2
Dec 13 '18 at 20:40
source share

try this, it uses basic C.

 timeval = struct.pack('ll', 2, 100) s.setsockopt(socket.SOL_SOCKET,SO_RCVTIMEO, timeval) 
+1
Aug 29 '17 at 9:00
source share



All Articles