簡體   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