简体   繁体   中英

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...?

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.

  1. Open input TCP port. 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: 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. I recommend you can refer to this blog to configure your idleTimeoutInMinutes value.

Please try to check your project and configuration. Any further findings, please let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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