13

I'm looking at http://docs.python-requests.org/en/latest/ and "Connection Timeouts" is listed as a feature. However, when I read further, it states

timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds).

That doesn't sound like a description of a connection timeout. What I'm seeing is a connection is successful, it uploads a big file and then waits for a response. However, the response takes a while and then timesout.

How can I set a connection timeout, but still wait for slow responses once a connection has been successful? Thanks a lot.

chevett
  • 659
  • 2
  • 7
  • 18
  • 1
    Related: [Python requests, how to limit received size, transfer rate, and/or total time?](http://stackoverflow.com/q/22346158), [Timeout for python requests.get entire response](https://stackoverflow.com/q/21965484) – Martijn Pieters Jul 16 '14 at 15:41

2 Answers2

17

The requests (for humans) library has connection timeouts, see - https://requests.kennethreitz.org/en/master/user/advanced/#timeouts

r = requests.get('https://github.com', timeout=(3.05, 27))

# e.g. explicitly
conn_timeout = 6
read_timeout = 60
timeouts = (conn_timeout, read_timeout)
r = requests.get('https://github.com', timeout=timeouts)

The docs are not exactly explicit about which value is which in the tuple, but it might be safe to assume that it's (connect, read) timeouts.

Darren Weber
  • 1,537
  • 19
  • 20
  • This is the most practical answer to setting timeouts – Bruno Grieder Mar 05 '21 at 07:42
  • 1
    The docs is not explicit, but looking the [code docstring](https://github.com/psf/requests/blob/2d5517682b3b38547634d153cea43d48fbc8cdb5/requests/sessions.py#L476-L478) we can confirm that it is `(connect, read)` – caiolopes Apr 22 '22 at 16:50
12

The timeout is used for both the socket connect stage and the response reading stage. The only exception is streamed requests; if you set stream=True, the timeout cannot be applied to the reading portion. The timeout is indeed used just for waiting for the socket to connect or data to be received.

If you need an overall timeout, then use another technique, like using interrupts or eventlets: Timeout for python requests.get entire response

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343