脚本重启后现有连接错误

P. *_*dre 7 python python-3.x

我在Python 3.7上执行HTTP请求,但是如果我在执行期间遇到一些错误并且脚本停止,则所有新的HTTP请求(甚至在重新启动脚本之后)都会出现错误:[Errno 10054] An existing connection was forcibly closed by the remote host.

我必须禁用/启用我的网络,以删除错误.这可能是由于我的PC/OS,因为该脚本适用于RaspberryPi但不适用于我的Windows 10.但我不知道如何修复它.

这里是生成错误的最小代码:

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))
Run Code Online (Sandbox Code Playgroud)

一旦出现错误,如果我想安装新模块,即使是pip.exe也会返回相同的错误.

edit1:我尝试了别的东西:

我有另一个脚本只执行SQL请求,它完美地工作.

但是一旦我有错误,即使这个有问题: "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:我在Windows上的Ubuntu上安装了bash,我在同一台PC上做同样的事情,但是"OS不同",我得到了ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

我试过其他PC(Windows 7),和我一样的问题.

@djvg我检查了openSSL版本(1.1.0i)和TSL版本(1.2),因此它看起来与您提供的链接兼容.这是pip冻结的输出(感谢提示,我不知道顺便说一句):

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我没有使用"特殊网络",如果我在我的专业网络上测试,它不起作用,但有另一个错误(可能是代理问题),所以我使用另一个没有特殊安全性的网络.但我不认为是安全问题,因为脚本停止后会出现此错误.这个脚本在raspberry pi上效果很好.可能是操作系统问题?像网络板第一次没有正确关闭连接.但我不知道如何核实......

@UserX我无法禁用防火墙:公司政治.我同意你的看法,这可能就是原因.是否可以打开另一个连接?或者即使脚本崩溃也要强制关闭?

P. *_*dre 0

我回答我自己的问题:

就我而言,这是防火墙问题。由于公司政策,我无法修改其参数,但在没有防火墙的计算机上它可以工作。它不是一个软件解决方案,也是一个昂贵的解决方法,但总比没有好。