简体   繁体   中英

Connection reset by peer w/ Python's Request Module. Curl and Postman work fine.

Python 2.7.10

I'm making a very basic API call to my gitlab repo.

I can successfully curl the endpoint and I can succesfully call it with Postman

However it fails when trying to use Python's request module.

Curl Works:

curl https://gitlab.example.com/api/v4/projects

  • Outputs the expected JSON response from the endpoint (no auth needed)

Postman I also get the expected result: 邮递员电话工作

However when I submit via requests: [Cert is self signed, hence the verify=False]

r = requests.get("https://gitlab.example.com/api/v4/projects", verify=False)

Error thrown:

Traceback (most recent call last):
  File "./repo_analyzer.py", line 36, in <module>
    r = requests.get("https://xxxxxxxxxxxx/api/v4/projects", verify=False)
  File "/Library/Python/2.7/site-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/adapters.py", line 490, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))

I'm a bit at a loss on why it's not working with python, but works otherwise.

After searching a ton of blogs and posts about this, it seemed that I needed to update requests[security] package through pip.

I was getting errors running both pip install -U requests[security] and sudo pip install -U requests[security] so I started from scratch:

  1. Downloaded Python 2.7 and reinstalled from the official site
  2. pip install requests
  3. pip install -U requests
  4. pip install -U requests[security]

After that, the calls were successful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM