When sending HTTP request, python encountered: error 54, ‘connection reset by peer’ solution

When sending HTTP request, python encountered: error 54, ‘connection reset by peer’ solution

 

background

The company will change all the Intranet environment from HTTP access to HTTPS access, and need to issue the self-made CA [certificate authority] to all the machines accessing the Intranet environment in order to log in to the Intranet environment.
When you need to send an HTTP request to the server to get some data, you will report an error “connection reset by peer”. The code is as follows:

f_path = '/tmp/ca.cert.pem'

def add_ca():
    # Importing ca certificates
    f = open(f_path, 'w+')
    ca = "-----BEGIN CERTIFICATE-----xxxxx-----END CERTIFICATE-----"
    f.writelines(ca)

url = 'https://jira.xx.local/rest/api/2/search'
res = requests.post(url, json=text, headers={"Authorization": "xxxx"}, verify=f_path)

reason

The OpenSSL library is too old. The URL you are requesting is not compatible.

Important

If more than one version of Python is installed in the system at the same time, please check which version of Python your code uses before taking the solution.

How to Fix

      1. the best way is to upgrade Python to 2.7. X or above, which will solve this problem. If you don’t want to upgrade python, you can use:

    PIP install requests [security]

      1. another outdated method:

    PIP install pyopenssl NDG httpclient pyasn1

Using certificate embedded code can save the trouble of installing CA manually for each machine

def add_ca():
    # Importing ca certificates
    f = open(f_path, 'w+')
    ca = "-----BEGIN CERTIFICATE----------END CERTIFICATE-----"
    f.writelines(ca)

This CA certificate is mandatory when sending HTTP request:

 res = requests.post(url, json=text, headers={"Authorization": "xxxxx"},verify=f_path)
 #f_path is the path to the ca certificate

Read More: