繁体   English   中英

消息后,套接字断开连接(远程主机拒绝连接),使用python

[英]Socket disconnected after a message, (Connection refused by remote host), using python

我已经在azure上上传了我的python套接字(云服务项目),并且每当我连接到Hercules客户端套接字时……一两则消息之后,连接就被远程主机强制关闭了……?

服务器代码

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
import SocketServer
class IphoneChat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
        print('clients are'), self.factory.clients                  
    def connectionLost(self, reason):
        self.factory.clients.remove(self)        
    def dataReceived(self, data):
        msg = ""
        msg =  data.strip()
        for c in self.factory.clients:
            c.message(msg)                  
    def message(self, message):
        self.transport.write(message + '\n')        

print ('Iphone Chat server startedd')
factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(9073, factory)
reactor.run()

我试图在我的环境中重现您的问题,但失败了。 根据我的经验,如果在天蓝色工作者角色中使用套接字,我们经常要注意3点。

  1. 打开输入TCP端口。 如果打开端口作为侦听器端口,则最好将此端口添加到端点设置中。
    在此处输入图片说明

  2. 考虑到工作者角色的状态,我建议我们可以将逻辑编码到while True循环函数中,如下所示:

     while True: reactor.listenTCP(65534, f) print "server run" reactor.run() sleep(1.0) 
  3. 根据错误消息,我想原因是天蓝色的负载平衡器将在240秒内杀死空闲连接。 我建议您可以参考此博客来配置您的idleTimeoutInMinutes值。

请尝试检查您的项目和配置。 任何进一步的发现,请让我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM