Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

http - Python requests.exception.ConnectionError: connection aborted "BadStatusLine"

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.

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. enter image description here 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:Usersgarvitabpythonlibsite-packages
equestsapi.py", line 65, in
 get
return request('get', url, **kwargs)
File "C:Usersgarvitabpythonlibsite-packages
equestsapi.py", line 49, in
request
response = session.request(method=method, url=url, **kwargs)
File "C:Usersgarvitabpythonlibsite-packages
equestssessions.py", line 4
61, in request
resp = self.send(prep, **send_kwargs)
File "C:Usersgarvitabpythonlibsite-packages
equestssessions.py", line 5
73, in send
r = adapter.send(request, **kwargs)
 File "C:Usersgarvitabpythonlibsite-packages
equestsadapters.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...