Why do I receive a timeout error from Pythons requests module?

前端 未结 3 1462
轮回少年
轮回少年 2020-12-30 01:57

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

相关标签:
3条回答
  • 2020-12-30 02:06

    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.

    0 讨论(0)
  • 2020-12-30 02:07

    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"
    

    )

    0 讨论(0)
  • 2020-12-30 02:11
    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
    
    0 讨论(0)
提交回复
热议问题