简体   繁体   English

扭曲的Python以获取长数据

[英]Python Twisted for long data

Python coders Python编码器

I am working with twisted for build a server, which receive 3000 bytes of data on each connection, my issue is that packages are been truncated and stored on database as on packages pieces, What I am looking for, is a way to solve this kind of data packages that must be parsed as a one long data. 我正在与twisted一起构建服务器,该服务器在每个连接上接收3000字节的数据,我的问题是包裹被截断并像包裹件一样存储在数据库中,我正在寻找的是一种解决此类问题的方法必须解析为一个long数据的数据包。

Line received is not a way, cause this kind of data is sent withuot a delimiter, Then I am thinking on a way of loop, however I am not completely sure of it or how to implement 行接收不是一种方式,导致这种数据没有定界符发送,那么我正在考虑一种循环方式,但是我不确定它或如何实现

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 is stream-oriented. TCP是面向流的。 See the FAQ entry for this topic . 请参阅有关此主题的FAQ条目

If you want to buffer up 3000 bytes before processing, see twisted.protocols.stateful.StatefulProtocol . 如果要在处理之前缓冲3000个字节,请参见twisted.protocols.stateful.StatefulProtocol For example: 例如:

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