本文整理汇总了Python中buffer.Buffer.write方法的典型用法代码示例。如果您正苦于以下问题:Python Buffer.write方法的具体用法?Python Buffer.write怎么用?Python Buffer.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类buffer.Buffer
的用法示例。
在下文中一共展示了Buffer.write方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PseudoStreamDecoder
# 需要导入模块: from buffer import Buffer [as 别名]
# 或者: from buffer.Buffer import write [as 别名]
class PseudoStreamDecoder(Stream):
def __init__(self, transport_class, transport_kwargs):
self.bufin=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
self.bufout=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
self.upstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
self.downstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
self.transport=transport_class(self, **transport_kwargs)
self.lockin=threading.Lock()
self.lockout=threading.Lock()
def decode_data(self, data):
with self.lockin:
#print "decoding %s"%repr(data)
self.bufin.drain()
self.bufin.write(data)
self.transport.downstream_recv(self.bufin)
cookie=self.bufin.cookie
self.bufin.cookie=None
return self.upstream.read(), cookie
def encode_data(self, data, cookie):
with self.lockout:
#print "encoding %s"%repr(data)
self.bufout.drain()
self.bufout.write(data)
self.bufout.cookie=cookie
self.transport.upstream_recv(self.bufout)
return self.downstream.read()
示例2: __init__
# 需要导入模块: from buffer import Buffer [as 别名]
# 或者: from buffer.Buffer import write [as 别名]
class Session:
"""
The session makes sure that all sent messages are queued until they receive a response.
"""
headerframe = None
default = None
def __init__(self, peer):
self.server = peer.server
self.peer = peer
## queue containing all received data
self.buffer = Buffer();
self.tx = 0
self.rx = 0
## just a small optimization for slow connections
self.expectsize = None
## all running periodcalls
self._periods = list();
assert self.headerframe is not None, "headerframe is None"
if self.default:
self.switch(self.default)
self.sessionInit();
def period(self, time, cb, *args, **kw):
period = task.LoopingCall(cb, *args, **kw)
period.start(time)
self._periods.append(period)
def later(self, cb, *args, **kw):
reactor.callLater(0, cb, *args, **kw)
def check_recv(self):
"""
Check receiver queue and pack the header frame when possible.
"""
headersize = self.headerframe._size()
while self.buffer.has(self.expectsize):
# nothing to do since we have not received any header frames yet...
if not self.buffer.has(headersize):
self.debug("Buffer does not contain enough data for header (no payload)")
return
data = self.buffer.read(headersize, buffered=True)
header = self.headerframe()._unpack(data)
if not self.validateHeader(header):
self.debug("Validation of header frame failed")
return self.lose()
totalsize = headersize + self.getPayloadSize(header)
if not self.buffer.has(totalsize):
## do not read header if it has already been done, we know next header + payload
self.expectsize = totalsize
self.debug("Buffer does not contain enough data for header + payload")
return
# flush header, header and payload is usually sent in the same packet
# this should be more optimized than sending seperately
data = self.buffer.read(totalsize)[headersize:]
self.rx += totalsize
if not self.validatePayload(header, data):
self.debug("Validation of frame failed, losing connection")
return self.lose();
self.receiveFrame(header, data)
self.expectsize = None
return
def recv(self, data):
self.buffer.write(data)
self.check_recv();
def send(self, frame):
self.prepareFrame(frame)
header = self.buildHeaderFrame(self.headerframe, frame)
if header is NotImplemented:
self.debug("Session's buildHeaderFrame method not implemented!")
return self.lose()
data = header + frame._pack()
self.tx += len(data)
self.sendMessage(data)
return True
def lose(self):
#.........这里部分代码省略.........