Twisted似乎没有调用dataReceived

时间:2014-05-10 06:58:21

标签: python twisted

我一直在编写一个概念验证类型程序,它通过网络发送屏幕截图,由于某种原因,dataRecieved方法在发送数据后没有被调用。这是我的代码

服务器

from twisted.internet import protocol, reactor
from twisted.internet.endpoints import TCP4ServerEndpoint
from PIL import Image

class ImageProto(protocol.Protocol):
    def dataReceived(self, data):
        print("test")
        print(isinstance(Image.open(data), Image.Image))
class ImageProtoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return ImageProto()
endpoint = TCP4ServerEndpoint(reactor, 1336)
endpoint.listen(ImageProtoFactory())
reactor.run()

客户端

from twisted.internet.protocol import Protocol
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
from PIL import ImageGrab
from io import BytesIO

class ImageSender(Protocol):
    def connectionMade(self):
        i = ImageGrab.grab()
        i.resize((1280, 720))
        output = BytesIO()
        i.save(output, format = "png")
        output.flush()
        self.transport.write(output.getvalue())
        self.transport.loseConnection()
        reactor.stop()
point = TCP4ClientEndpoint(reactor, "localhost", 1336)
d = connectProtocol(point, ImageSender())
reactor.run()

1 个答案:

答案 0 :(得分:2)

当您在reactor.stop结束时致电connectionMade时,您会在发送任何数据之前立即关闭整个过程。删除它,你应该没事。