简体   繁体   中英

Twisted appears to not call dataReceived

I have been writing a proof-of-concept type program that sends a screenshot over the network, and for some reason, the dataRecieved method isn't being called after data is sent. Here is my code

SERVER

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

CLIENT

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

When you call reactor.stop at the end of connectionMade , you immediately shut down the whole process before any data is sent. Remove it and you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM