def spawn_read(): message = self.stream.read_with_checker(Box().check) # print "message, len: %s, content: %r" % (len(message), message) if message: req_box = Box(message) rsp_box = Box() print rsp_box rsp_box.cmd = req_box.cmd rsp_box.ret = 1000 rsp_box.version = 333 rsp_box.body = 'ok' buf = rsp_box.pack() self.stream.write(buf[:10]) self.stream.write(buf[10:]) if self.stream.closed(): print 'client closed' # 说明客户端断掉链接了 return
def index(request): print request.box box = Box() # box = request.box box.ret = 100 box.body = "wokao" * 1000 print box.header_len print box.packet_len print box # request.write(box.pack()) # return box.version = 219 box.flag = 199 buf = box.pack() print '1' request.write(buf[:10]) time.sleep(2) print '2' request.write(buf[10:])
# -*- coding: utf-8 -*- import sys sys.path.insert(0, '../../') from netkit.contrib.tcp_client import TcpClient from netkit.box import Box import time client = TcpClient(Box, '127.0.0.1', 9900, timeout=5) client.connect() box = Box() box.cmd = 1 # box.cmd = 101 box.body = '我爱你' client.write(box) t1 = time.time() while True: # 阻塞 box = client.read() print 'time past: ', time.time() - t1 print box if not box: print 'server closed' break
def write_to_users(request): tmp_box = Box() tmp_box.cmd = 99 tmp_box.body = "bd" request.close_users([-2])
# -*- coding: utf-8 -*- from netkit.box import Box box1 = Box() box1.body = '我爱你' print box1 print repr(box1.pack()) print box1.body buf = box1.pack() box2 = Box() for i in range(0, len(buf) + 1): tmp_buf = buf[0:i] if box2.unpack(tmp_buf) > 0: print box2 break
# -*- coding: utf-8 -*- from netkit.box import Box box1 = Box() box1.body = '我爱你' print box1 print repr(box1.pack()) print box1.body box2 = Box() box2.unpack(box1.pack()) print box2 print box2.body print box2.magic