def handle(uid): # client = TcpClient(Box, '192.168.1.67', 29000, timeout=None) client = TcpClient(Box, '115.28.224.64', 29000, timeout=None) client.connect() box = Box() box.cmd = 1 box.set_json(dict( uid=uid )) client.write(box) box = Box() box.cmd = 2 box.set_json(dict( uid=uid )) 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 handle(uid): # client = TcpClient(Box, '192.168.1.67', 29000, timeout=None) client = TcpClient(Box, '115.28.224.64', 29000, timeout=None) client.connect() box = Box() box.cmd = 1 box.set_json(dict(uid=uid)) client.write(box) box = Box() box.cmd = 2 box.set_json(dict(uid=uid)) 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 handle(): trigger = Trigger(forwarder_input_address_list=config.FORWARDER_INPUT_ADDRESS_LIST) box = Box() box.cmd = 3 trigger.write_to_users([ [(constants.CONNS_AUTHED,), box] ]) box2 = Box() box2.cmd = 5 trigger.write_to_worker( box2 )
def index(request): print '\n', 'req', request.box request.box.ret = 100 request.box.body = request.box.body[::-1] print 'rsp', request.box, '\n' request.write(request.box) box = Box() box.cmd = 15 print 'event', box, '\n' request.write(box)
def main(): # client = TcpClient(Box, '192.168.1.67', 29000, timeout=None) client = TcpClient(Box, '115.28.224.64', 29000, timeout=None) client.connect() box = Box() box.cmd = 3 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 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 handle(uid): client = TcpClient(Box, '127.0.0.1', 29000, timeout=None) client.connect() box = Box() box.cmd = 100 box.set_json(dict( uid=uid )) 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 handle(uid): client = TcpClient(Box, config.GATEWAY_OUTER_HOST, config.GATEWAY_OUTER_PORT, timeout=None) client.connect() box = Box() box.cmd = 2 # box.cmd = 999 box.set_json(dict(uid=uid)) 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
# -*- 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
import time import logging import socket logger = logging.getLogger('netkit') logger.addHandler(logging.StreamHandler()) logger.setLevel(logging.DEBUG) address = ('127.0.0.1', 7777) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(address) stream = Stream(s) box = Box() box.cmd = 32 box.body = '我爱你' stream.write(box.pack()) while True: # 阻塞 buf = stream.read_with_checker(Box().unpack) if buf: print Box(buf) if stream.closed(): print 'server closed' break
def write_to_users(request): tmp_box = Box() tmp_box.cmd = 99 tmp_box.body = "bd" request.close_users([-2])