繁体   English   中英

扭曲的Python以获取长数据

[英]Python Twisted for long data

Python编码器

我正在与twisted一起构建服务器,该服务器在每个连接上接收3000字节的数据,我的问题是包裹被截断并像包裹件一样存储在数据库中,我正在寻找的是一种解决此类问题的方法必须解析为一个long数据的数据包。

行接收不是一种方式,导致这种数据没有定界符发送,那么我正在考虑一种循环方式,但是我不确定它或如何实现

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是面向流的。 请参阅有关此主题的FAQ条目

如果要在处理之前缓冲3000个字节,请参见twisted.protocols.stateful.StatefulProtocol 例如:

class LingServer(StatefulProtocol):
    def getInitialState(self):
        return self.ling, 3000

    def ling(self, data):
        # Process here, len(data) == 3000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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