最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • python使用requests语句报错的原因有哪些

    正文概述    2020-01-10   267

    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. 代理读取超时

    说明与代理建立连接成功,代理也发送请求到目标站点,但是代理读取目标站点资源超时 

    即使代理访问很快,如果代理服务器访问的目标站点超时,这个锅还是代理服务器背 

    假定代理可用,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',))

    起源地下载网 » python使用requests语句报错的原因有哪些

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元