def main(): f = open('141018.txt') while True: line = f.readline() if not line: break log = line.strip().lstrip('{').rstrip('}') #print(log) info = log.split(',') k_v = {} for data in info: element = data.split(': ') if len(element) <= 1: break key = element[0].strip().strip('\'') val = element[1].strip().strip('\'') k_v[key] = val if k_v['code'] == 'A': output = make_a(k_v) if k_v['code'] == 'B': output = make_b(k_v) print(output) printer.error(output) #break f.close() printer.wait_for_flush()
def do_login(self): global channel_center proto = self.client.proto req = proto.make_login_req() for i in range(self.max_retry): channel_center.login_request_increase() group, channel = channel_center.get_channel('login') channel_center.login_request_decrease() if channel == None: continue head = proto.make_ssl_head(server_dict[group]['login']['name']) info_val = channel_center.pyget(channel, req, head) if info_val == None: continue if info_val['status'] != 200: printer.error('client %s fd_login status %s' % (self.client.bidno, info_val['status'])) continue ack_val = proto.parse_login_ack(info_val['body']) if 'pid' not in ack_val or 'name' not in ack_val: printer.error('client %s fd_login ack error %s' % (self.client.bidno, str(info_val))) continue self.client.pid_login = ack_val['pid'] self.client.name_login = ack_val['name'] printer.warning( 'client %s login %s %s' % (self.client.bidno, ack_val['name'], ack_val['pid'])) return True printer.warning('client %s login failed !!!') return False
def update_status(self): global global_info udp_recv = self.recv_udp() if udp_recv == None: return udp_recv = self.proto.parse_decode(udp_recv) info_val = self.proto.parse_ack(udp_recv) if info_val == None: return code = info_val['code'] if code == 'F': printer.debug(udp_recv) return if code == 'C': printer.info(udp_recv) return if code == 'A' or code == 'B': printer.warning(udp_recv) printer.error(udp_recv) return printer.error(udp_recv) return
def do_image(self): global channel_center proto = self.client.proto req = proto.make_image_req(self.price) if self.count == 0: channel = 'tb0' else: channel = 'tb1' for i in range(self.max_retry): group, handle = channel_center.get_channel(channel) if handle == None: continue head = proto.make_ssl_head(server_dict[group]['toubiao']['name']) info_val = channel_center.pyget(handle, req, head) if info_val == None: continue if info_val['status'] != 200: printer.error('client %s fd_image status %s' % (self.client.bidno, info_val['status'])) continue ack_sid = proto.get_sid_from_head(info_val['head']) ack_val = proto.parse_image_ack(info_val['body']) if ack_sid == None or ack_sid == '': break if 'image' not in ack_val: printer.error('client %s fd_image ack error %s' % (self.client.bidno, str(info_val))) break if ack_val['image'] == None or ack_val['image'] == '': break self.client.sid_bid[self.count] = ack_sid self.client.picture_bid[self.count] = ack_val['image'] break self.event_finish.set()
def do_price(self): global channel_center proto = self.client.proto sid = self.client.sid_bid[self.count] number = self.client.number_bid[self.count] req = proto.make_price_req(self.price, number) if self.count == 0: channel = 'tb0' else: channel = 'tb1' for i in range(self.max_retry): group, handle = channel_center.get_channel(channel, self.group) if handle == None: continue head = proto.make_ssl_head(server_dict[group]['toubiao']['name'], sid) info_val = channel_center.pyget(handle, req, head) if info_val == None: continue if info_val['status'] != 200: printer.error('client %s fd_price status %s' % (self.client.bidno, info_val['status'])) continue ack_val = proto.parse_price_ack(info_val['body']) if 'errcode' in ack_val: printer.error('client %s fd_price ack error %s' % (self.client.bidno, str(ack_val))) if ack_val['errcode'] == '112': self.client.err_112[self.count] = True break if 'price' in ack_val: self.client.price_bid[self.count] = ack_val['price'] break break
def pyget(self, handle, req, headers = {}): time_req = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S.%f') printer.info(time_req + ' :: ' + str(headers) + ' :: ' + req) ack = None try: handle.request('GET', req, headers = headers) except KeyboardInterrupt: self.close_handle(handle) return False except: printer.critical(format_exc()) self.close_handle(handle) return False try: ack = handle.getresponse() except KeyboardInterrupt: self.close_handle(handle) return False except: printer.critical(format_exc()) self.close_handle(handle) return False self.close_handle(handle) try: body = ack.read() except KeyboardInterrupt: del(ack) return None except: printer.critical(format_exc()) del(ack) return None key_val = {} key_val['head'] = str(ack.msg) key_val['status'] = ack.status try: key_val['body'] = body.decode('gb18030') except: printer.error(body) key_val['body'] = '' time_ack = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S.%f') printer.info(time_ack + ' :: ' + str(key_val['head']) + ' :: ' + str(key_val['body'])) printer.time(time_req + ' --- ' + time_ack + ' :: ' + str(headers) + ' :: ' + req + ' :: ' + str(key_val['head']) + ' :: ' + str(key_val['body'])) del(ack) return key_val
def parse_ssl_ack(self, string): xml_string = string.strip() key_val = {} try: root = ElementTree.fromstring(xml_string) for child in root: key_val[child.tag] = child.text except : printer.error(xml_string) if key_val == {} : printer.error(xml_string) return key_val
def wait_for_finish(self, timeout=None): waittime = timeout if timeout != None else self.timeout_decode if self.event_finish.wait(waittime + 2) == True: # XXX 多等待2秒 return True else: self.lock_timeout.acquire() self.flag_timeout = True self.lock_timeout.release() printer.error('client %s bid %d fd_decode Timeout' % (self.client.bidno, self.count)) sleep(0) return False
def parse_image_ack(self, buff): string = buff.decode('gb18030') info_val = self.parse_ssl_ack(string) key_val = {} if info_val['ERRORCODE'] != '0': key_val['errcode'] = info_val['ERRORCODE'] key_val['errstring'] = info_val['ERRORSTRING'] printer.error(string) else: #key_val['image'] = self.image_decode(info_val['IMAGE_CONTENT']) key_val['image'] = info_val['IMAGE_CONTENT'] return key_val
def parse_login_ack(self, buff): string = buff.decode('gb18030') info_val = self.parse_ssl_ack(string) key_val = {} if 'ERRORCODE' in info_val: key_val['errcode'] = info_val['ERRORCODE'] key_val['errstring'] = info_val['ERRORSTRING'] printer.error(string) else: key_val['name'] = info_val['CLIENTNAME'] key_val['pid'] = info_val['PID'] return key_val
def parse_price_ack(self, buff): string = buff.decode('gb18030') info_val = self.parse_ssl_ack(string) key_val = {} if 'ERRORCODE' in info_val: key_val['errcode'] = info_val['ERRORCODE'] key_val['errstring'] = info_val['ERRORSTRING'] printer.error(string) else: key_val['time'] = info_val['BIDTIME'] key_val['count'] = info_val['BIDCOUNT'] key_val['price'] = info_val['BIDAMOUNT'] key_val['name'] = info_val['CLIENTNAME'] key_val['bidno'] = info_val['BIDNUMBER'] return key_val
def do_proc(self, arg): key_val, callback = arg price = key_val['image_price'] proto = proto_ssl_image(key_val) req = proto.make_image_req(price) head = proto.make_ssl_head(self.host_name) info_val = self.pyget(req, head) #logger.debug(sorted(info_val.items())) if info_val == None: return printer.info(sorted(info_val.items())) if info_val['status'] != 200: logger.error('ack status error : %s' % info_val['status']) printer.error('ack status error : %s' % info_val['status']) try: logger.error(info_val['body'].decode('gb18030')) printer.error(info_val['body'].decode('gb18030')) except: pass else: return try: logger.error(info_val['body'].decode()) printer.error(info_val['body'].decode()) except: pass else: return logger.error('unknow body coding') printer.error(info_val['body']) return ack_sid = proto.get_sid_from_head(info_val['head']) ack_val = proto.parse_image_ack(info_val['body']) ack_val['sid'] = ack_sid ack_val['price'] = price #printer.debug(sorted(ack_val.items())) #logger.debug(sorted(ack_val.items())) logger.debug(ack_sid) if self.flag_stop == True: logger.error( 'image ack recived but thread be stoped. Do not proc callback.' ) return if callback != None: callback(ack_val)
def do_proc(self, arg): key_val, callback = arg proto = proto_ssl_login(key_val) req = proto.make_login_req() head = proto.make_ssl_head(self.host_name) info_val = self.pyget(req, head) logger.debug(sorted(info_val.items())) if info_val == None: return printer.info(sorted(info_val.items())) if info_val['status'] != 200: logger.error('ack status error : %s' % info_val['status']) printer.error('ack status error : %s' % info_val['status']) try: logger.error(info_val['body'].decode('gb18030')) printer.error(info_val['body'].decode('gb18030')) except: pass else: return try: logger.error(info_val['body'].decode()) printer.error(info_val['body'].decode()) except: pass else: return logger.error('unknow body coding') printer.error(info_val['body']) return ack_sid = proto.get_sid_from_head(info_val['head']) ack_val = proto.parse_login_ack(info_val['body']) #ack_val['sid'] = ack_sid #printer.debug(sorted(ack_val.items())) #logger.debug(sorted(ack_val.items())) if callback != None: callback(ack_val)
def do_proc(self, arg): key_val, callback = arg price = key_val['shot_price'] image = key_val['image_decode'] sid = key_val['sid'] event = key_val['event'] delay = key_val['delay'] worker_in = key_val['worker_in'] worker_out= key_val['worker_out'] group = self.group worker_in(group) proto = proto_ssl_price(key_val) req = proto.make_price_req(price, image) head = proto.make_ssl_head(self.host_name, sid) int_price = int(price) event.wait() if delay != 0 : sleep(delay) global current_price cur_price = current_price.get() logger.debug('shot_price %d , cur_price %d ' % (int_price, cur_price)) if int_price > cur_price + 300 or int_price < cur_price - 300: worker_out(group) return info_val = self.pyget(req, head) #logger.debug(sorted(info_val.items())) if info_val == None: worker_out(group) return printer.info(sorted(info_val.items())) if info_val['status'] != 200 : logger.error('ack status error : %s' % info_val['status']) printer.error('ack status error : %s' % info_val['status']) try: logger.error(info_val['body'].decode('gb18030')) printer.error(info_val['body'].decode('gb18030')) except: pass else: worker_out(group) return try: logger.error(info_val['body'].decode()) printer.error(info_val['body'].decode()) except: pass else: worker_out(group) return logger.error('unknow body coding') printer.error(info_val['body']) worker_out(group) return ack_sid = proto.get_sid_from_head(info_val['head']) ack_val = proto.parse_price_ack(info_val['body']) #ack_val['sid'] = ack_sid #printer.debug(sorted(ack_val.items())) #logger.debug(sorted(ack_val.items())) worker_out(group) if callback != None : callback(ack_val)