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.
from Existing connection error after script restart
No comments:
Post a Comment