I do HTTP requests on Python 3.7, but if I have some errors during the execution and the script stops, all new HTTP requests (even after restart the script) have the errors: [Errno 10054] An existing connection was forcibly closed by the remote host
.
I have to disable/enable my network, to remove the error. It's probably due to my PC/OS because the script works on RaspberryPi but not on my Windows 10. But I don't know how to fix it.
Here the minimum code to generate the error:
import requests
import json
import urllib.request
import socket
if __name__ == '__main__':
params = json.dumps({"toto": "ABCD"}).encode('utf-8')
try:
head = {'content-type': 'application/json'}
#replace http by https, to generate the error, re-writte http, and it will never work again
url = 'http://www.google.com'
with requests.post(url, data=params, headers=head) as response:
print("All is OK: " + str(response))
except (urllib.error.URLError, socket.timeout) as e:
print("Error time out: " + str(e.args))
except Exception as e:
print("Uknown error: " + str(e.args))
Once I have the error, even pip.exe return the same error if I want to install a new module.
edit1:I tried something else:
I have another script which does only SQL requests, it works perfectly.
But once I have the error even this one has the problem:
"Lost connection to MySQL server during query (%s)" % (e,))
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')
edit2: I installed bash on Ubuntu on Windows, and I do the same thing on the same PC but "OS different", I get ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
I tried on other PC (Windows 7), same problem as mine.
@djvg I checked openSSL version (1.1.0i) and TSL version (1.2), so it looks compatible with the link you give. And this is the output of pip freeze (thanks for the tips, I didn't know btw):
certifi==2018.11.29 chardet==3.0.4 cycler==0.10.0 idna==2.8 kiwisolver==1.0.1 matplotlib==3.0.2 numpy==1.15.4 PyMySQL==0.9.3 pyparsing==2.3.0 python-dateutil==2.7.5 requests==2.21.0 six==1.12.0 urllib3==1.24.1
@M.Spiller I don't use a "special network", if I test on my professional network, it doesn't work but with another error (probably proxy issue), so I use another network with no particular security. But I don't think is a security issue, because this error appears once the script stops. And the script works well on raspberry pi. Could be an OS issue ? Like the network board doesn't close the connection correctly the first time. But I don't know how to verify that...
@UserX I can't disable the firewall: company politic. I'm agree with you, it's probably the cause. Is it possible to open another connection ? Or to force the closing even when the script crash ?