簡體   English   中英

扭曲的客戶端和服務器tcp傳輸不發送整個文件

[英]twisted not sending entire file with twisted client and server tcp transfer

編輯:由於我是通過文本追加文件的,因此未正確保存,因此我決定重寫我最初希望的方式並將文件另存為流:Twisted服務器:

from twisted.internet import reactor, protocol
import os,json

class Echo(protocol.Protocol):
    f = file
    def dataReceived(self, data):
        try:
            try:
                print format(json.loads(data))
                print "got jason"
                self.f=open("test.png","wb")

                self.transport.write("ready")
            except:
                print "filedata incoming!"
                self.f.write(data)
        except:
            print "unknown error" #happens if we don't receive json first

    def connectionLost(self, reason):
        if self.f!=file:self.f.close()

def main():
    """This runs the protocol on port 8000"""
    factory = protocol.ServerFactory()
    factory.protocol = Echo
    reactor.listenTCP(8000,factory)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

下面的原始帖子

Twisted發送了大約99.9%的文件,然后就這樣發送了,我想我寫的文件不正確。

扭曲服務器:

from twisted.internet import reactor, protocol
import os,json

class Echo(protocol.Protocol):

    def dataReceived(self, data):
        try:
            print format(json.loads(data))
            print "got jason"
            self.transport.write("ready")
        except:
            print "filedata incoming!"
            f = open("test.png","a")
            f.write(data)
            f.close()


def main():
    """This runs the protocol on port 8000"""
    factory = protocol.ServerFactory()
    factory.protocol = Echo
    reactor.listenTCP(8000,factory)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

扭曲的客戶:

from twisted.internet import reactor, protocol
import os,json

fname="pic.png"

class EchoClient(protocol.Protocol):
    """Once connected, send a message, then print the result."""

    def connectionMade(self):

        fsize = os.path.getsize(fname) 
        self.transport.write(json.dumps({"file":{"size":fsize}}))

    def sendFile(self):
        print "sending file" 
        f = open(fname,"rb")
        self.transport.write(f.read())
        f.close()
        print "closing conn"
        self.transport.loseConnection()

    def dataReceived(self, data):
        "As soon as any data is receive"
        print "Server said: ", data
        self.sendFile()


    def connectionLost(self, reason):
        print "connection lost"

class EchoFactory(protocol.ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"
        reactor.stop()


# this connects the protocol to a server runing on port 8000
def main():
    f = EchoFactory()
    reactor.connectTCP("localhost", 8000, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

基本上,服務器正在運行並正在偵聽,客戶端連接並立即發送json,服務器接收數據包並告知發送客戶端“確定”,然后客戶端發送文件。 然后服務器接收文件並將其寫入磁盤。 我只是測試一下東西,所以該程序可能沒有多大意義,尤其是使用文件附加-但我注意到,在傳輸和最終寫入之后,該文件的大小與原始文件大小幾乎相同,但並不完全相同。小於300字節,因此幾乎沒有用。 我發送的文件不正確嗎? 還是只是寫錯了? 哦,是的,我正在同一台計算機上測試服務器和客戶端。

最終,我計划在兩台本地計算機之間來回發送最大1GB的文件,以進行備份,並且希望將這些文件作為數據流寫入,我不喜歡我使用的append方法,但是我不知道如何在不首先實際打開文件的情況下引用文件對象,而這是我第一次收到json對象時只想做的事情。

謝謝!

您正在打開“ test.png”以添加文本。 那是故意的嗎?

您也有一個裸露的except ,這是一個壞主意,因為它捕獲了所有異常。 僅捕獲您期望的那些異常。

問題是您期望dataReceived一次接收所有數據。 這不是Internet的工作方式: 請參閱Twisted FAQ,以獲取有關為何如此以及如何修復代碼的解釋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM