Python requests.exception.ConnectionError:连接中止“BadStatusLine”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28557466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python requests.exception.ConnectionError: connection aborted "BadStatusLine"
提问by BajajG
I am trying to use the Python requests module to issue Http GET commands to access some REST based APIs. The urls are working fine on a RESTClient but when I use the same url in python, I get a connection error.
我正在尝试使用 Python requests 模块发出 Http GET 命令来访问一些基于 REST 的 API。这些 url 在 RESTClient 上运行良好,但是当我在 python 中使用相同的 url 时,出现连接错误。
The code I am trying to execute is:
我试图执行的代码是:
payload={"mac":new_mac,"token":token}
userloginurl="http://192.168.1.40:9119/uid"
r=requests.get(userloginurl,params=payload)
print(r.url)
If I test this url using RESTClient, I get a 200 OK status code in the response header along with some more fields.
But this is not working with python requests. The traceback of the error is shown below:
如果我使用 RESTClient 测试这个 url,我会在响应头中得到一个 200 OK 状态代码以及更多字段。
但这不适用于 python 请求。错误的回溯如下所示:
Traceback (most recent call last):
File "getAPids.py", line 34, in <module>
r=requests.get(userloginurl,params=payload)
File "C:\Users\garvitab\python\lib\site-packages\requests\api.py", line 65, in
get
return request('get', url, **kwargs)
File "C:\Users\garvitab\python\lib\site-packages\requests\api.py", line 49, in
request
response = session.request(method=method, url=url, **kwargs)
File "C:\Users\garvitab\python\lib\site-packages\requests\sessions.py", line 4
61, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\garvitab\python\lib\site-packages\requests\sessions.py", line 5
73, in send
r = adapter.send(request, **kwargs)
File "C:\Users\garvitab\python\lib\site-packages\requests\adapters.py", line 4
15, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",
))
I looked up for the cause of the problem. Possibly, the response received is not formatted correctly. Is there a way to handle this problem?
我查找了问题的原因。可能是收到的响应格式不正确。有没有办法处理这个问题?
Thanks in advance.
提前致谢。
采纳答案by BajajG
The problem was with the url. This connection was meant to be established over https and I was using http in the python script. Hence the issue.
问题出在网址上。这个连接是通过 https 建立的,我在 python 脚本中使用了 http。因此问题。
回答by Michael
Have you actually checked, what gets send over the wire? I suppose you might have to convert your dictionary to a JSON string by yourself, or use the json=
keyword instead of payload=
. See http://docs.python-requests.org/en/latest/user/quickstart/#custom-headersfor details. This may do the trick:
您是否真的检查过,通过电线发送了什么?我想您可能必须自己将字典转换为 JSON 字符串,或者使用json=
关键字而不是payload=
. 有关详细信息,请参阅http://docs.python-requests.org/en/latest/user/quickstart/#custom-headers。这可能会奏效:
import json
payload = json.dumps({"mac":new_mac,"token":token})
userloginurl = "http://192.168.1.40:9119/uid"
r = requests.get(userloginurl, data=payload)
print(r.url)