2013-04-25 41 views
0

的Python程序員Python的扭曲長期數據

我與扭曲爲建立一個服務器,它接收每個連接上3000個字節數據的工作,我的問題是,包被截斷,並作爲封裝件存儲在數據庫,我正在尋找的是解決這種數據包的一種方法,它必須被解析爲一個長數據。收到

線不是辦法,使這種數據的withuot定界符發送出去後,我想上環的方式,但我不能完全肯定這一點或如何實現

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() 

回答

1

TCP是面向流的。見the FAQ entry for this topic

如果要在處理之前緩衝3000個字節,請參閱twisted.protocols.stateful.StatefulProtocol。例如:

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

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