def heart_beat(self): msg = { 'uid' : 911, } cmd = 1001 pack_msg = tcp_pack_helper.pack_msg(cmd, json_helper.dumps(msg)) self.transport.write(pack_msg)
def heart_beat(self): msg = { 'uid': 911, } cmd = 1001 pack_msg = tcp_pack_helper.pack_msg(cmd, json_helper.dumps(msg)) self.transport.write(pack_msg)
def connectionMade(self): ''' 客户端连接成功之后会自动调用该方法 :return: ''' print 'is already connect to the server' msg = { 'uid' : 911, 'pwd' : 'dkjfkdjfkdjfkd', } cmd = 1000 #定义1000表示登陆 pack_msg = tcp_pack_helper.pack_msg(cmd, json_helper.dumps(msg)) self.transport.write(pack_msg) t = task.LoopingCall(self.heart_beat) t.start(10, now=True)
def send_msg_to_worker(self, data): try: routing_keys_worker = config.WORKER_QUEUE_NAME channel = self.get_channel() msg = Content(json_helper.dumps(data)) msg["delivery mode"] = 1 exchange_name_worker = config.EXCHANGE_NAME_WORKER d = channel.basic_publish( exchange=exchange_name_worker, content=msg, routing_key=routing_keys_worker) print '=====send msg to worker====' print msg print '===========================' d.addErrback(self.errb) except: print traceback.format_exc()
def send_msg_to_worker(self, data): try: routing_keys_worker = config.WORKER_QUEUE_NAME channel = self.get_channel() msg = Content(json_helper.dumps(data)) msg["delivery mode"] = 1 exchange_name_worker = config.EXCHANGE_NAME_WORKER d = channel.basic_publish(exchange=exchange_name_worker, content=msg, routing_key=routing_keys_worker) print '=====send msg to worker====' print msg print '===========================' d.addErrback(self.errb) except: print traceback.format_exc()
def connectionMade(self): ''' 客户端连接成功之后会自动调用该方法 :return: ''' print 'is already connect to the server' msg = gps_helper.read() #msg = { # 'uid' : 911, # 'pwd' : 'dkjfkdjfkdjfkd', #} cmd = 1000 #定义1000表示登陆 pack_msg = tcp_pack_helper.pack_msg( cmd, json_helper.dumps(gps_helper.read())) t = task.LoopingCall(self.heart_beat) t2 = task.LoopingCall(self.transport.write(pack_msg)) t.start(10, now=True) t2.start(2, now=True)
def http_post_json(url, req_data=None, header=None): code = -1 error = '' data = {} req_data = {} if req_data is None else req_data header = {} if header is None else header request = urllib2.Request(url) request.add_header('Content-Type', 'application/json') for k, v in header.items(): request.add_header(k, v) try: resp = urllib2.urlopen(request, json_helper.dumps(req_data)).read() data = json_helper.loads(resp) except HTTPError, e: code = e.code error = e.strerror
def send_async_msg(topic=KAFKA_TOPIC_NAME, msg=None): ''' 发送KAFKA消息 :param topic: :param msg: :return: ''' logging.info('kafka send async msg, date: {}'.format( datetime.datetime.now())) config_info = GLOBAL_CONFIG.get('ASYNC_MESSAGE_BROKER') client = KafkaClient( hosts=config_info.get('config').get('hosts'), zookeeper_hosts=config_info.get('config').get('zookeeper_hosts'), broker_version=config_info.get('config').get('broker_version')) """ topic = client.topics[topic] with topic.get_sync_producer() as producer: producer.produce(json_helper.dumps(msg)) return 'success' """ topic = client.topics[topic] producer = topic.get_sync_producer() return producer.produce(json_helper.dumps(msg))
import datetime import simplejson as json import json_helper class Entity(object): def __init__(self): super(Entity, self).__init__() self.param = 'val' self.dt = datetime.datetime.now() self.num = 51 def __str__(self): return 'Entity(param=%s,dt=%s,num=%d)' % (self.param, self.dt, self.num) Entity = json_helper.setup_from_dict(Entity) TYPES = json_helper.define_json_classes(Entity) #s = json.dumps(Entity(), cls=CustomTypeEncoder) o = Entity() print o s = json_helper.dumps(o, TYPES) #s = json.dumps(o, cls=make_encoder(TYPES)) print s #print json.loads(s, object_hook=make_decoder(TYPES)) print json_helper.loads(s, TYPES)