title: How to set a timeout for the connections in HTTPConnectionPool when using requests
body: <p>I have the following script:</p>
  <pre><code>from requests import Session
  from time import sleep
  
  
  session = Session()
  url = 'http://tsetmc.com'
  r = session.get(url, timeout=5)  # 200 OK
  
  sleep(200)  # if the idle time is greater than ~120 seconds, then the next `session.get` attempt will fail
  
  # the following line fails with 
  # `requests.exceptions.ReadTimeout: HTTPConnectionPool(host='tsetmc.com', port=80): Read timed out. (read timeout=5)` 
  # but will work if I use headers={&quot;Connection&quot;: &quot;close&quot;} parameter
  session.get(url, timeout=5)
  </code></pre>
  <p>It fails with:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;...\Python310\lib\site-packages\urllib3\connectionpool.py&quot;, line 449, in _make_request
      six.raise_from(e, None)
    File &quot;&lt;string&gt;&quot;, line 3, in raise_from
    File &quot;...\Python310\lib\site-packages\urllib3\connectionpool.py&quot;, line 444, in _make_request
      httplib_response = conn.getresponse()
    File &quot;...\Python310\lib\http\client.py&quot;, line 1374, in getresponse
      response.begin()
    File &quot;...\Python310\lib\http\client.py&quot;, line 318, in begin
      version, status, reason = self._read_status()
    File &quot;...\Python310\lib\http\client.py&quot;, line 279, in _read_status
      line = str(self.fp.readline(_MAXLINE + 1), &quot;iso-8859-1&quot;)
    File &quot;...\Python310\lib\socket.py&quot;, line 705, in readinto
      return self._sock.recv_into(b)
  TimeoutError: timed out
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;...\Python310\lib\site-packages\requests\adapters.py&quot;, line 440, in send
      resp = conn.urlopen(
    File &quot;...\Python310\lib\site-packages\urllib3\connectionpool.py&quot;, line 785, in urlopen
      retries = retries.increment(
    File &quot;...\Python310\lib\site-packages\urllib3\util\retry.py&quot;, line 550, in increment
      raise six.reraise(type(error), error, _stacktrace)
    File &quot;...\Python310\lib\site-packages\urllib3\packages\six.py&quot;, line 770, in reraise
      raise value
    File &quot;...\Python310\lib\site-packages\urllib3\connectionpool.py&quot;, line 703, in urlopen
      httplib_response = self._make_request(
    File &quot;...\Python310\lib\site-packages\urllib3\connectionpool.py&quot;, line 451, in _make_request
      self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
    File &quot;...\Python310\lib\site-packages\urllib3\connectionpool.py&quot;, line 340, in _raise_timeout
      raise ReadTimeoutError(
  urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='tsetmc.com', port=80): Read timed out. (read timeout=5)
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;F:\stock\stock-market-scripts\ETFs\temp\sessiontest3.py&quot;, line 14, in &lt;module&gt;
      session.get(url, timeout=5)
    File &quot;...\Python310\lib\site-packages\requests\sessions.py&quot;, line 542, in get
      return self.request('GET', url, **kwargs)
    File &quot;...\Python310\lib\site-packages\requests\sessions.py&quot;, line 529, in request
      resp = self.send(prep, **send_kwargs)
    File &quot;...\Python310\lib\site-packages\requests\sessions.py&quot;, line 645, in send
      r = adapter.send(request, **kwargs)
    File &quot;...\Python310\lib\site-packages\requests\adapters.py&quot;, line 532, in send
      raise ReadTimeout(e, request=request)
  requests.exceptions.ReadTimeout: HTTPConnectionPool(host='tsetmc.com', port=80): Read timed out. (read timeout=5)
  
  Process finished with exit code 1
  </code></pre>
  <p>As you can see the second call to <code>session.get</code> will raise an error if there has been a significant idle time during the script run.</p>
  <p>I believe the cause of this error is a server configuration that causes connections to be dropped after a certain period of time (~120s), but <a href="https://2.python-requests.org/en/master/user/advanced/#keep-alive" rel="nofollow noreferrer">requests tries to reuse</a> the expired connection.</p>
  <p>I have also noticed that passing <code>headers={&quot;Connection&quot;: &quot;close&quot;}</code> argument to <code>session.get</code> will fix the issue, but I believe that also means the connections won't be sent back to the pool?</p>
  <p>My question: Is there a way to set a <code>timeout</code> for the connections in the HTTPConnectionPool so that they won't be used if expired?</p>
  <p>(
  other options I can think of are:</p>
  <ul>
  <li>the <code>headers={&quot;Connection&quot;: &quot;close&quot;}</code> above (inefficient?)</li>
  <li>using <code>threading.Timer</code> to create and use a new session object which I belive will have a fresh connection pool. (sounds too complicated?)</li>
  <li>catch the error and retry (too slow?)
  )</li>
  </ul>