yav*_*avg 2 python timeout exception connection-timeout python-requests
这是我的场景。我正在向在某些情况下需要太长时间的网页发出请求。我希望当超过 10 秒没有从服务器发出响应时,请求被取消但没有收到任何错误。这是我当前的代码和我看到的错误。出现此错误时,我的代码结束。总之,我想发出一个 Web 请求并限制在 10 秒内没有答案,我完成请求并继续我的代码。
requests.post("www.webpage.com", headers = {'Content-type': 'application/x-www-form-urlencoded'}, data = {"conid":1,"event":5},timeout=10)
.
.
.
Run Code Online (Sandbox Code Playgroud)
当 10 秒过去时,我收到此错误
ReadTimeout: HTTPConnectionPool(host='www.webpage.com', port=80): Read timed out. (read timeout=10)
Run Code Online (Sandbox Code Playgroud)
出于保密原因,我没有放置真实的 url,但通常没有设置超时,它可以工作
使用 anexception来处理超时
try:
requests.post("www.webpage.com", headers = {'Content-type': 'application/x-www-form-urlencoded'}, data = {"conid":1,"event":5},timeout=10)
except requests.exceptions.ReadTimeout:
print("Server didn't respond within 10 seconds")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
396 次 |
| 最近记录: |