针对长数据的Python Twisted
Python编码器 我正在使用twisted构建一个服务器,它在每个连接上接收3000字节的数据,我的问题是包被截断并存储在数据库中,就像在包块上一样,我正在寻找的是一种解决这种数据包的方法,这种数据包必须作为一个长数据进行解析 接收到的行不是一种方式,因为这种数据是用分隔符发送的,所以我考虑使用循环方式,但我不完全确定它或如何实现针对长数据的Python Twisted,python,networking,twisted,Python,Networking,Twisted,Python编码器 我正在使用twisted构建一个服务器,它在每个连接上接收3000字节的数据,我的问题是包被截断并存储在数据库中,就像在包块上一样,我正在寻找的是一种解决这种数据包的方法,这种数据包必须作为一个长数据进行解析 接收到的行不是一种方式,因为这种数据是用分隔符发送的,所以我考虑使用循环方式,但我不完全确定它或如何实现 from twisted.internet.protocol import Factory, Protocol from twisted.internet impo
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
import binascii
from Datagram import *
class LingServer(Protocol):
def __init__(self):
print 'Staring Ling Server'
pass
def connectionMade(self):
try:
print 'Accepted connection'
except ValueError:
print "Oops! Connection was not started"
def connectionLost(self, reason):
print "Connection lost ", reason
def dataReceived(self, data):
try:
print "Data received ", data
data = binascii.hexlify(data)
Datagram (header=data[:10], content=data[10:])
session.commit()
#self.transport.write(self.decoder.processDatagram(data))
except ValueError:
print "Oops! That was no valid data. Try again..."
class LingFactory(Factory):
def __init__(self):
pass
def buildProtocol(self, addr):
return LingServer()
reactor.listenTCP(12345, LingFactory())
reactor.run()
TCP是面向流的。看 如果要在处理前缓冲3000字节,请参阅
twisted.protocols.stateful.StatefulProtocol
。例如:
class LingServer(StatefulProtocol):
def getInitialState(self):
return self.ling, 3000
def ling(self, data):
# Process here, len(data) == 3000