gpt4 book ai didi

python - Twisted 似乎没有调用 dataReceived

转载 作者:行者123 更新时间:2023-12-01 05:14:14 28 4
gpt4 key购买 nike

我一直在编写一个概念验证类型的程序,该程序通过网络发送屏幕截图,并且由于某种原因,发送数据后没有调用 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()

最佳答案

当您在 connectionMade 结束时调用 reactor.stop 时,您会在发送任何数据之前立即关闭整个进程。删除它,你应该没问题。

关于python - Twisted 似乎没有调用 dataReceived,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23578111/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com