33

I use requests.post(url, headers, timeout=10) and sometimes I received a ReadTimeout exception HTTPSConnectionPool(host='domain.com', port=443): Read timed out. (read timeout=10)

Since I already set timeout as 10 seconds, why am I still receiving a ReadTimeout exception?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
chrizonline
  • 4,779
  • 17
  • 62
  • 102

3 Answers3

60

Per https://requests.readthedocs.io/en/latest/user/quickstart/#timeouts, that is the expected behavior. As royhowie mentioned, wrap it in a try/except block (e.g.:

try:
  requests.post(url, headers, timeout=10)
except requests.exceptions.Timeout:
  print "Timeout occurred"

)

Matthijs
  • 439
  • 3
  • 16
Foon
  • 6,148
  • 11
  • 40
  • 42
  • 1
    hi tk u for quick reply. this is a better answer because Catching this error will catch both ConnectTimeout and ReadTimeout errors. – chrizonline Feb 07 '15 at 01:21
  • @nuttynibbles You said in your question how to catch `ReadTimeout exception?`. Otherwise just only use `try/except` and catch all of them. – GLHF Feb 07 '15 at 01:22
  • sorry i am not sure why it is showing 0 vote here even though i upvote your ans – chrizonline Feb 07 '15 at 01:22
  • hi @howaboutNO, initially i thought by putting the timeout argument, it will auto stop the requests process if it takes longer than that – chrizonline Feb 07 '15 at 01:23
  • Using this answer, I wrote a function to deal with it: https://hastebin.com/izaponomer.py – xendi Sep 22 '18 at 19:27
  • This is great, however, how to continue in the loop? I am going through a page several times and record the response time in a loop and use try and except. But I would like it to go to the next run once a timeout occured. – IceQueeny Feb 25 '19 at 07:28
10
try:
    #defined request goes here
except requests.exceptions.ReadTimeout:
    # Set up for a retry, or continue in a retry loop

You can wrap it like an exception block like this. Since you asked for this only ReadTimeout. Otherwise catch all of them;

try:
    #defined request goes here
except:
    # Set up for a retry, or continue in a retry loop
GLHF
  • 3,835
  • 10
  • 38
  • 83
2

Another thing you can try is at the end of your code block, include the following:

time.sleep(2)

This worked for me. The delay is longer (in seconds) but might help overcome the issue you're having.

Attrey
  • 41
  • 7
  • 2
    It is not a good idea to use `time.sleep()` for something like this as suspends the thread that it is invoked from. Increasing the timeout is more viable in this case. – Eren Atas Sep 01 '22 at 08:33