簡體   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