zoukankan      html  css  js  c++  java
  • Python报错ConnectionError: connection aborted BadStatusLine解决

    问题

    云端项目上有一个api通过HTTP/GET请求调用返回json数据
    使用Python自带requests库发送GET请求查询数据报错如下

    requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('HTTP/1.1 0 
    '))
    

    分析

    使用Chrome浏览器可以正常显示返回值,F12检察元素查看GET返回Headers内容。

    HTTP/1.1 0
    Content-Length: 21269
    Connection: Keep-Alive
    Server: ApiServer
    

    显然Response Headers中status code给错了
    看了眼response内容本身没问题
    暂时屏蔽status code校验优先对内容进行测试

    解决

    将status code强制赋值为200抑制错误
    溯源response函数得知调用关系为requests -> urllib -> http
    重写class比较复杂,直接修改ConnectionError源码位于python/lib/http/client.py > HTTPResponse > _read_status
    找到

    # The status code is a three-digital number
    try:
        status = int(status)
    

    改为

    # The status code is a three-digital number
    try:
        status = int(status) or 200
    

    OK!测完记得改回来

  • 相关阅读:
    设计模式之三:Abstract Factory(转)
    设计模式之二:adapter模式(转)
    设计模式之一:设计原则(转)
    双链表操作
    单链表操作
    C#-Activex插件操作指南
    积分源码上线
    換友情鏈接
    企业短信群发
    掉了,全掉了。
  • 原文地址:https://www.cnblogs.com/azureology/p/13613200.html
Copyright © 2011-2022 走看看