簡體   English   中英

扭曲發送文件。 python

[英]Twisted sending files. python

我正在嘗試使用 Twisted 通過 .network 傳輸圖像和其他文件。 我為此使用 class“FileSender”,特別是我在服務器上使用的方法“beginFileTransfer”。 但是客戶端沒有完全接收到文件,我無法打開它。 同時,如果我發送一個小文件,它就會出現。 所以問題是大小。 你能告訴我如何發送大文件嗎? 以下是服務器和客戶端代碼:

from twisted.internet.protocol import Protocol, connectionDone
from twisted.python import failure
from twisted.protocols.basic import FileSender
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor


class TestServer(Protocol):

    def connectionMade(self):
        filesender = FileSender()
        f = open('00000.jpg', 'rb')
        filesender.beginFileTransfer(f, self.transport)


    def dataReceived(self, data: bytes):
        data = data.decode('UTF-8')
        print(data)

    def connectionLost(self, reason: failure.Failure = connectionDone):
        print("Server lost Connection")

class QOTDFactory(Factory):
    def buildProtocol(self, addr):
        return TestServer()

# 8007 is the port you want to run under. Choose something >1024
endpoint = TCP4ServerEndpoint(reactor, 8007, interface="127.0.0.1")
endpoint.listen(QOTDFactory())
reactor.run()

from twisted.internet.protocol import Protocol, ClientFactory, connectionDone
from sys import stdout
from twisted.protocols.basic import FileSender

from twisted.python import failure


class TestClient(Protocol):
    def connectionMade(self):
        print("Client did connection")

    def dataReceived(self, data):
        f = open('13.jpg', 'wb')
        f.write(data)
        self.transport.write("Client take connection".encode('UTF-8'))

    def connectionLost(self, reason: failure.Failure = connectionDone):
        print("Client lost Connection Protocol")

class EchoClientFactory(ClientFactory):
    def startedConnecting(self, connector):
        print('Started to connect.')

    def buildProtocol(self, addr):
        print('Connected.')
        return TestClient()

    def clientConnectionLost(self, connector, reason):
        print('Lost connection factory.  Reason:', reason)

    def clientConnectionFailed(self, connector, reason):
        print('Connection failed factory. Reason:', reason)

from twisted.internet import reactor
reactor.connectTCP('127.0.0.1', 8007, EchoClientFactory())
reactor.run()

Twisted 使用非阻塞套接字操作:寫入或讀取 sockets 的數據剛好足以不阻塞。 Filesender 實際上會發送數據塊,直到所有數據都發送完畢,您需要緩沖它們直到它們完成。

我會把服務器部分寫成:

class TestServer(Protocol):
    
    def connectionMade(self):
        filesender = FileSender()
        f = open('00000.jpg', 'rb')
        d = filesender.beginFileTransfer(f, self.transport)
        d.addCallback(lambda _: self.transport.loseConnection()) # signals end of transmission

然后客戶端為:

class TestClient(Protocol):

    def __init__(self):
        self.f = open('13.jpg', 'wb')

    def connectionMade(self):
        print("Client did connection")

    def dataReceived(self, data):
        self.f.write(data) # I would buffer all the received data and
                           # write them all at once for efficiencey
                           # but this one will do

    def connectionLost(self, reason: failure.Failure = connectionDone):
        self.f.close() # server is done sending all chunks, close the file
        print("Client lost Connection Protocol")

HTH

暫無
暫無

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

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