为什么我从Pythons请求模块收到超时错误?

nut*_*les 17 python python-requests

我用requests.post(url, headers, timeout=10),有时我收到了ReadTimeout exception HTTPSConnectionPool(host='domain.com', port=443): Read timed out. (read timeout=10)

由于我已将超时设置为10秒,为什么我仍然收到ReadTimeout异常?

Foo*_*oon 30

根据http://docs.python-requests.org/en/latest/user/quickstart/#timeouts,这是预期的行为.正如royhowie所提到的,将它包装在try/except块中(例如:

try:
  requests.post(url, headers, timeout=10)
except requests.exceptions.Timeout:
  print "Timeout occurred"
Run Code Online (Sandbox Code Playgroud)

)


GLH*_*LHF 6

try:
    #defined request goes here
except requests.exceptions.ReadTimeout:
    # Set up for a retry, or continue in a retry loop
Run Code Online (Sandbox Code Playgroud)

您可以像这样将其包装为异常块。由于您只要求这样做ReadTimeout。否则,抓住所有人;

try:
    #defined request goes here
except:
    # Set up for a retry, or continue in a retry loop
Run Code Online (Sandbox Code Playgroud)

  • 嗨 tk 你的快速答复。我会那样做 (2认同)