Python requests.exception.ConnectionError:connection aborted" BadStatusLine" [英] Python requests.exception.ConnectionError: connection aborted "BadStatusLine"

查看:5498
本文介绍了Python requests.exception.ConnectionError:connection aborted" BadStatusLine"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python请求模块发出Http GET命令来访问一些基于REST的API。网址在RESTClient上工作正常但是当我在python中使用相同的url时,我收到连接错误。

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.

我试图执行的代码是:

payload={"mac":new_mac,"token":token}
userloginurl="http://192.168.1.40:9119/uid"
r=requests.get(userloginurl,params=payload)
print(r.url)

如果我使用RESTClient测试这个url,我会在响应头中获得200 OK状态代码以及更多字段。

但这不适用于python请求。错误的回溯如下所示:

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:

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?

提前致谢。

推荐答案

问题在于网址。此连接旨在通过https建立,我在python脚本中使用http。因此问题。

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.

这篇关于Python requests.exception.ConnectionError:connection aborted&quot; BadStatusLine&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆