【发布时间】:2014-01-17 21:39:59
【问题描述】:
我正在使用 Twisted 创建一个简单的 TCP 服务器。使用 Twisted 协议,是否可以指定 dataReceived 返回的确切字节数?
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
class TestProtocol(Protocol):
def connectionMade(self):
print(id(self))
self.transport.write('hello')
def connectionLost(self, reason):
print('connection lost called')
def dataReceived(self, data):
# is it possible to specify size of "data"?
print('data received called')
class TestFactory(Factory):
protocol = TestProtocol
endpoint = TCP4ServerEndpoint(reactor, 8007)
endpoint.listen(TestFactory())
reactor.run()
我问这个问题是因为如果我可以控制传入的字节数,我可以避免处理单个 dataReceived 回调中传入的部分协议消息或多个协议消息。
我可以通过在 recv() 方法中指定确切的字节数来使用 asynccore 实现这一点。如果能在 Twisted 中也能做到这一点,那就太好了。
谢谢
...艾伦
【问题讨论】: