亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Twisted 服務器/客戶端之間的多個調用/響應消息

Twisted 服務器/客戶端之間的多個調用/響應消息

瀟瀟雨雨 2021-11-16 16:26:51
我有一個在 Twisted 中實現的基本服務器和客戶端。我的目標是讓客戶端處理一些數據,將其進度報告給服務器,然后重復直到處理完所有數據??蛻舳四軌蛳蚍掌靼l送初始消息,但它沒有收到服務器的響應,讓它知道可以開始處理該數據。這是我的代碼。服務器:from twisted.internet import reactor, protocolPORT = 9000progress = 0class MyServer(protocol.Protocol):    def dataReceived(self, data):        global progress        print(data)        if data != "Ready?":            progress = int(data)        self.transport.write("Got it.")        self.transport.loseConnection()    def connectionLost(self, reason):        global progress        if progress == 10:            print("Completed.")            reactor.stop()class MyServerFactory(protocol.Factory):    protocol = MyServerfactory = MyServerFactory()reactor.listenTCP(PORT, factory)reactor.run()客戶:from twisted.internet import reactor, protocolimport timeHOST = 'localhost'PORT = 9000progress = 0class MyClient(protocol.Protocol):    def connectionMade(self):        self.transport.write("Ready?")        self.transport.loseConnection()    def dataReceived(self, data):        global progress        progress += 1        print(progress)        self.transport.write(str(progress))        self.loseConnection()    def connectionLost(self, reason):        global progress        if progress == 10:            print("Completed.")            reactor.stop()class MyClientFactory(protocol.ClientFactory):    protocol = MyClientfactory = MyClientFactory()reactor.connectTCP(HOST, PORT, factory)reactor.run()
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

我發現我的問題是我過早地關閉了連接。在一些早期的測試中,我試圖在dataReceived函數中發送多條消息,這些消息被連接成一條消息。這讓我相信您必須斷開連接才能通過每條消息。相反,您只需要在發送另一條消息之前讓函數完成即可。這是按預期工作的更新代碼。


服務器:


from twisted.internet import reactor, protocol


PORT = 9000


progress = 0


class MyServer(protocol.Protocol):


    def dataReceived(self, data):

        global progress

        print(data)

        if data != "Ready?":

            progress = int(data)

        self.transport.write("Got it")

        if progress == 10:

            self.transport.loseConnection()


    def connectionLost(self, reason):

        print("Completed.")

        reactor.stop()


class MyServerFactory(protocol.Factory):

    protocol = MyServer


factory = MyServerFactory()

reactor.listenTCP(PORT, factory)

reactor.run()

客戶:


from twisted.internet import reactor, protocol

import time


HOST = 'localhost'

PORT = 9000


progress = 0


class MyClient(protocol.Protocol):


    def connectionMade(self):

        self.transport.write("Ready?")


    def dataReceived(self, data):

        global progress

        progress += 1

        print(progress)

        self.transport.write(str(progress))

        if progress == 10:

            self.transport.loseConnection()


    def connectionLost(self, reason):

        print("Completed.")

        reactor.stop()


class MyClientFactory(protocol.ClientFactory):

    protocol = MyClient


factory = MyClientFactory()

reactor.connectTCP(HOST, PORT, factory)


reactor.run()



查看完整回答
反對 回復 2021-11-16
  • 1 回答
  • 0 關注
  • 253 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號