Question

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()
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top