K2LL33D SHELL

 Apache/2.4.7 (Ubuntu)
 Linux sman1baleendah 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64
 uid=33(www-data) gid=33(www-data) groups=33(www-data)
 safemode : OFF
 MySQL: ON | Perl: ON | cURL: OFF | WGet: ON
  >  / usr / lib / python2.7 / dist-packages / twisted / protocols /
server ip : 104.21.89.46

your ip : 108.162.241.18

H O M E


Filename/usr/lib/python2.7/dist-packages/twisted/protocols/stateful.py
Size1.67 kb
Permissionrw-r--r--
Ownerroot : root
Create time27-Apr-2025 09:56
Last modified14-Feb-2011 11:45
Last accessed06-Jul-2025 23:36
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
# -*- test-case-name: twisted.test.test_stateful -*-

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


from twisted.internet import protocol

try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO

class StatefulProtocol(protocol.Protocol):
"""A Protocol that stores state for you.

state is a pair (function, num_bytes). When num_bytes bytes of data arrives
from the network, function is called. It is expected to return the next
state or None to keep same state. Initial state is returned by
getInitialState (override it).
"""
_sful_data = None, None, 0

def makeConnection(self, transport):
protocol.Protocol.makeConnection(self, transport)
self._sful_data = self.getInitialState(), StringIO(), 0

def getInitialState(self):
raise NotImplementedError

def dataReceived(self, data):
state, buffer, offset = self._sful_data
buffer.seek(0, 2)
buffer.write(data)
blen = buffer.tell() # how many bytes total is in the buffer
buffer.seek(offset)
while blen - offset >= state[1]:
d = buffer.read(state[1])
offset += state[1]
next = state[0](d)
if self.transport.disconnecting: # XXX: argh stupid hack borrowed right from LineReceiver
return # dataReceived won't be called again, so who cares about consistent state
if next:
state = next
if offset != 0:
b = buffer.read()
buffer.seek(0)
buffer.truncate()
buffer.write(b)
offset = 0
self._sful_data = state, buffer, offset