Python报错ConnectionError: connection aborted BadStatusLine解决

时间:2020-09-04 15:07:24   收藏:0   阅读:873

问题

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

requests.exceptions.ConnectionError: (‘Connection aborted.‘, BadStatusLine(‘HTTP/1.1 0 \r\n‘))

分析

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

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

显然Response Headers中Status Code给错了
看了一下response内容本身没问题
希望暂时屏蔽code校验对接口进行测试

解决

将status code强制赋值为200即刻抑制错误
考虑到调用关系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!测完记得改回来

原文:https://www.cnblogs.com/azureology/p/13613200.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!