简体   繁体   English

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

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

I have uploaded my python socket, (cloud service project), on azure ,and when ever I connected to Hercules client side socket ....after a message or two, connection closed by remote host... forcefully...? 我已经在azure上上传了我的python套接字(云服务项目),并且每当我连接到Hercules客户端套接字时……一两则消息之后,连接就被远程主机强制关闭了……?

Server Code 服务器代码

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()

I tried to reproduce your issue on my environment, but failed. 我试图在我的环境中重现您的问题,但失败了。 Base on my experience, we often pay attention to 3 points if we use socket in azure worker role. 根据我的经验,如果在天蓝色工作者角色中使用套接字,我们经常要注意3点。

  1. Open input TCP port. 打开输入TCP端口。 If we open a port as listener port, we'd better to add this port into the endpoints setting. 如果打开端口作为侦听器端口,则最好将此端口添加到端点设置中。
    在此处输入图片说明

  2. Considered the worker role status, I suggest we can code logic into the while True loop function, as following, 考虑到工作者角色的状态,我建议我们可以将逻辑编码到while True循环函数中,如下所示:

     while True: reactor.listenTCP(65534, f) print "server run" reactor.run() sleep(1.0) 
  3. According to the error message, I guess the reason is that azure load balancer will kill the idle connection in 240s. 根据错误消息,我想原因是天蓝色的负载平衡器将在240秒内杀死空闲连接。 I recommend you can refer to this blog to configure your idleTimeoutInMinutes value. 我建议您可以参考此博客来配置您的idleTimeoutInMinutes值。

Please try to check your project and configuration. 请尝试检查您的项目和配置。 Any further findings, please let me know. 任何进一步的发现,请让我知道。

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

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