知行编程网知行编程网  2022-09-15 12:30 知行编程网 隐藏边栏  599 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python使用requests语句报错的原因有哪些的相关知识,包括python async,以及python循环语句while这些编程知识,希望对大家有参考作用。

python使用requests语句报错的原因是什么

python中使用requests语句报错的原因主要有以下几种情况:

1 连接超时

服务器在指定时间内没有应答,抛出 requests.exceptions.ConnectTimeout

requests.get('http://github.com', timeout=0.001)

# 抛出错误

requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='github.com', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7f1b16da75f8>, 'Connection to github.com timed out. (connect timeout=0.001)'))

2. 连接、读取超时

如果分别指定连接超时和读取超时,则服务器在指定时间内没有响应并抛出 requests.exceptions.ConnectTimeout

- timeout=([连接超时时间], [读取超时时间])

- 连接:客户端连接服务器并并发送http请求服务器

- 读取:客户端在服务器发送第一个字节之前等待的时间

requests.get('http://github.com', timeout=(6.05, 0.01))
# 抛出错误
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='github.com', port=80): Read timed out. 
(read timeout=0.01)

3. 未知的服务器

抛出 requests.exceptions.ConnectionError
requests.get('http://github.comasf', timeout=(6.05, 27.05))
# 抛出错误
requests.exceptions.ConnectionError: HTTPConnectionPool(host='github.comasf', port=80): Max retries exceeded 
with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f75826665f8>: 
Failed to establish a new connection: [Errno -2] Name or service not known',))

4. 代理连接不上

代理服务器拒绝建立连接,端口拒绝连接或未打开,抛出 requests.exceptions.ProxyError

requests.get('http://github.com', timeout=(6.05, 27.05), proxies={"http": "192.168.10.1:800"})
# 抛出错误
requests.exceptions.ProxyError: HTTPConnectionPool(host='192.168.10.1', port=800): Max retries exceeded with 
url: http://github.com/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.
connection.HTTPConnection object at 0x7fce3438c6d8>: Failed to establish a new connection: [Errno 111] 
Connection refused',)))

5. 连接代理超时

代理服务器没有响应 requests.exceptions.ConnectTimeout

requests.get('http://github.com', timeout=(6.05, 27.05), proxies={"http": "10.200.123.123:800"})
# 抛出错误
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='10.200.123.123', port=800): Max retries exceeded 
with url: http://github.com/ (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 
0x7fa8896cc6d8>, 'Connection to 10.200.123.123 timed out. (connect timeout=6.05)'))

6. 代理读取超时

表示与proxy建立连接成功,proxy也向目标站点发送请求,但是proxy读取目标站点资源超时

即使代理访问非常快,如果代理服务器访问的目标站点超时,这个锅仍然是代理服务器的背。

假设代理可用,timeout是连接和读取过程到代理服务器的超时时间,不管代理服务器是否成功连接和读取

requests.get('http://github.com', timeout=(2, 0.01), proxies={"http": "192.168.10.1:800"})
# 抛出错误
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='192.168.10.1:800', port=1080): Read timed out. (read 
timeout=0.5)

7. 网络环境异常

可能是断网导致,抛出 requests.exceptions.ConnectionError

requests.get('http://github.com', timeout=(6.05, 27.05))
# 抛出错误
requests.exceptions.ConnectionError: HTTPConnectionPool(host='github.com', port=80): Max retries exceeded with 
url: / 
(Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc8c17675f8>: Failed to 
establish a new 
connection: [Errno -3] Temporary failure in name resolution',))

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享