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
-
-
- 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]
-
-
-
- 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:
- Python requests Error: ConnectionError: (‘Connection aborted.‘, error(104, ‘Connection reset by peer‘))
- Python raspberry pie starts sending IP address to mailbox
- [Solved] Python requests ConnectionError Error: connection aborted BadStatusLine
- How to Fix “HTTP error 403: forbidden” in Python 3. X
- Python Error: Failed to establish a new connection: [Errno -2] Name or service not known
- How to Fix Errors encountered in executing Python scripts with command line parameters
- ModuleNotFoundError: No module named ‘tensorflow.python’ And the pits encountered after installation
- [Solved] Connection Error: couldn‘t reach http://raw.githubusercontent.com/huggingface/…
- [Solved] ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memor
- [Solved] Conda install Error: An HTTP error occurred when trying to retrieve this URL. HTTP errors are often…
- [Solved] Request.url is not modifiable, use Request.replace() instead
- Python error: urllib.error.HTTPError : http Error 404: not found
- You can run the Ansible Playbook in Python by hand
- Python: RNN principle realized by numpy
- Python+Selenium Error: AttributeError: ‘WebDriver‘ NameError: name ‘By‘ is not defined
- [Solved] Python Selenium Error: AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘
- Solution for Python3.7 import gevent module error
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- The lenet model trained by Python failed to predict its own handwritten pictures