Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从服务器单元I重置客户端计算机拒绝连接_Python_Tcp_Client - Fatal编程技术网

Python 从服务器单元I重置客户端计算机拒绝连接

Python 从服务器单元I重置客户端计算机拒绝连接,python,tcp,client,Python,Tcp,Client,下面是我在服务中运行的代码。在大多数情况下,脚本可以正常运行数天/数周,直到脚本打嗝并崩溃。我不太担心崩溃的部分,因为我可以从错误日志中正确地解决问题。我面临的问题是,有时当服务重新启动并尝试再次连接到服务器时,会出现(10061,“连接被拒绝”)错误,因此服务无法再次启动。奇怪的是,当连接被拒绝时,没有python进程在运行。IE没有映像名为“pythonw.exe”或“pythonservice.exe”的进程。应该注意的是,在重置运行客户端脚本的计算机之前,我无法使用任何其他计算机连接到服

下面是我在服务中运行的代码。在大多数情况下,脚本可以正常运行数天/数周,直到脚本打嗝并崩溃。我不太担心崩溃的部分,因为我可以从错误日志中正确地解决问题。我面临的问题是,有时当服务重新启动并尝试再次连接到服务器时,会出现
(10061,“连接被拒绝”)
错误,因此服务无法再次启动。奇怪的是,当连接被拒绝时,没有python进程在运行。IE没有映像名为“pythonw.exe”或“pythonservice.exe”的进程。应该注意的是,在重置运行客户端脚本的计算机之前,我无法使用任何其他计算机连接到服务器。客户端计算机在windows server 2003操作系统上运行python 2.7。还应该注意的是,服务器是在一个硬件上编码的,我无法访问该硬件的代码

try:            
        EthernetConfig = ConfigParser()
        EthernetConfig.read('Ethernet.conf')

        HOST = EthernetConfig.get("TCP_SERVER", "HOST").strip()
        PORT = EthernetConfig.getint("TCP_SERVER", "PORT")

        lp = LineParser()
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        s.connect((HOST, PORT))
        reader = s.makefile("rb")

        while(self.run == True):

            line = reader.readline()
            if line:
                line = line.strip()
                lp.parse(line)
except:

        servicemanager.LogErrorMsg(traceback.format_exc()) # if error print it to event log
        s.shutdown(2)
        s.close()
        os._exit(-1)

连接被拒绝是一个错误,表示连接另一端的程序不接受您的连接尝试。很可能它没有注意到你的崩溃,也没有关闭它的连接

try:            
        EthernetConfig = ConfigParser()
        EthernetConfig.read('Ethernet.conf')

        HOST = EthernetConfig.get("TCP_SERVER", "HOST").strip()
        PORT = EthernetConfig.getint("TCP_SERVER", "PORT")

        lp = LineParser()
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        s.connect((HOST, PORT))
        reader = s.makefile("rb")

        while(self.run == True):

            line = reader.readline()
            if line:
                line = line.strip()
                lp.parse(line)
except:

        servicemanager.LogErrorMsg(traceback.format_exc()) # if error print it to event log
        s.shutdown(2)
        s.close()
        os._exit(-1)

你可以做的只是睡一会儿(30-60秒),然后再试一次,然后以循环的方式执行此操作,希望另一端注意到连接已断开,以便可以再次接受新连接。

结果是网络管理员关闭了我尝试连接的端口。它对属于服务器的一个IP是开放的。问题是服务器有两个网卡,两个单独的IP。这个问题现在已经解决了

+1延迟是个好主意,我会尝试一下。然而,它仍然不能解释为什么即使没有python进程打开,我仍然无法连接到服务器。即使过了一段时间。@Richard:不,是服务器拒绝了连接,你这边没有问题。谢谢你的帮助。我意识到服务器拒绝连接。阅读文档时,一次只允许2个连接。我不明白的是,当我重置客户端机器时,我能够再次连接到服务器,这让我相信客户端机器上的连接被占用了,尽管我没有证据证明确实如此。
try:            
        EthernetConfig = ConfigParser()
        EthernetConfig.read('Ethernet.conf')

        HOST = EthernetConfig.get("TCP_SERVER", "HOST").strip()
        PORT = EthernetConfig.getint("TCP_SERVER", "PORT")

        lp = LineParser()
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        s.connect((HOST, PORT))
        reader = s.makefile("rb")

        while(self.run == True):

            line = reader.readline()
            if line:
                line = line.strip()
                lp.parse(line)
except:

        servicemanager.LogErrorMsg(traceback.format_exc()) # if error print it to event log
        s.shutdown(2)
        s.close()
        os._exit(-1)