Twisted Python 发送二进制数据的问题

6 投票
1 回答
5562 浏览
提问于 2025-04-15 13:22

我想做的事情其实很简单:把一个文件从客户端发送到服务器。首先,客户端会发送文件的一些信息,比如文件的大小。然后,它会发送实际的文件。

这是我到目前为止所做的:

Server.py

from twisted.internet import reactor, protocol
from twisted.protocols.basic import LineReceiver

import pickle
import sys

class Echo(LineReceiver):

    def connectionMade(self):
        self.factory.clients.append(self)
        self.setRawMode()

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def lineReceived(self, data):
        print "line", data

    def rawDataReceived(self, data):
            try:
                obj = pickle.loads(data)
                print obj
            except:
                print data

        #self.transport.write("wa2")

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

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

Client.py

import pickle

from twisted.internet import reactor, protocol
import time
import os.path
from twisted.protocols.basic import LineReceiver

class EchoClient(LineReceiver):

    def connectionMade(self):
        file = "some file that is a couple of megs"
        filesize = os.path.getsize(file)
        self.sendLine(pickle.dumps({"size":filesize}))

        f = open(file, "rb")
        contents = f.read()
        print contents[:20]
        self.sendLine(contents[:20])
        f.close()

#        self.sendLine("hej")
#        self.sendLine("wa")

    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()

服务器只会输出反序列化后的对象:

{'size': 183574528L}

这是怎么回事?我想发送的20个字符去哪了?

如果我发送的是“hej”和“wa”,我会同时收到这两个(在同一条消息中,而不是两次)。

有人能帮忙吗?

1 个回答

9

你已经把服务器设置成了原始模式,也就是用 setRawMode() 这个方法,所以当有数据进来的时候,会调用 rawDataReceived 这个回调函数,而不是 lineReceived。如果你在 rawDataReceived 里打印收到的数据,你会看到所有的内容,包括文件的内容,但当你调用 pickle 来反序列化数据时,这些内容就被忽略了。

你可以选择改变发送数据到服务器的方式(我建议使用 netstring 格式),或者把内容放在 pickle 序列化的对象里,并且一次性完成这个操作。

self.sendLine(pickle.dumps({"size":filesize, 'content': contents[:20]}))

撰写回答