def get_fund_base_info(logger): tcp_client = tcp_connect() logger.info('get_fund_base_info Enter') mysql_cmd = { 'CmdNum': 2, 'Linear': True, 'Cmd': ['select', 'show'], 'Info': [{ 'Name': 'Fund' }, { 'Table': 'InfoSummary' }] } logger.info('Waiting Muxtex...') mutexFlag = mutex.acquire(True) logger.info('TCP Send %s' % (str(mysql_cmd))) tcp_client.send( alphabet.str2hexbytes(json.dumps(mysql_cmd, ensure_ascii=False))) res = tcp_recv(tcp_client, logger) if res != None: recv_json = json.loads(alphabet.hexbytes2str(res)) logger.info('TCP Recv %s' % (str(recv_json))) logger.info('Muxtex Release...') mutex.release() code_list = [] if res != None: recv_json = json.loads(alphabet.hexbytes2str(res)) logger.info('TCP Recv %s' % (str(recv_json))) res_list = recv_json['Result'][1] logger.info(res_list) for index_list in res_list: name = index_list[0] code = index_list[1] code_list.append(code) logger.info('Name:%s Code:%s' % (name, code)) for code_index in code_list: logger.info('Starting Process By Code:%s' % (code_index)) pt = multiprocessing.Process(target=spiderF.main_process, args=(code_index, logger)) pt.start() timer_sched(get_fund_base_info, argv=(logger, )) tcp_client.close()
def mysql_recv_process(client, recv_data): recv_str = alphabet.hexbytes2str(recv_data) recv_json = json.loads(recv_str) logger.info('Recv JSON:%s' % (str(recv_json))) cmd_num = recv_json.get('CmdNum', 0) linear_flag = recv_json.get('Linear', True) mydb = ml.mysql_client(host="182.61.47.202", user="******") res_dict = {'Result': []} if cmd_num > 0: cmd_list = recv_json.get('Cmd', None) logger.info("Recv cmd_list: %s" % (str(cmd_list))) info_list = recv_json.get('Info', None) logger.info("Recv info_list: %s" % (str(info_list))) else: res_dict['Result'].append("Command number under 1") logger.info("Command number under 1") for cmd_index in cmd_list: if (cmd_index == None): res_dict['Result'].append('cmd empty') if linear_flag == True: logger.info('Recv Empty CMD of JSON, Break') break else: logger.info('Recv Empty CMD of JSON, Continue') continue mysql_cb = callback_dict.get(cmd_index, None) if mysql_cb == None: res_dict['Result'].append("Can't find callback[%s]" % (cmd_index)) if linear_flag == True: logger.info("Can't find callback[%s], Break" % (cmd_index)) break else: logger.info("Can't find callback[%s], Continue" % (cmd_index)) continue if info_list != None: res = mysql_cb(mydb, info_list[cmd_list.index(cmd_index)]) else: res = mysql_cb(mydb, None) logger.info('Excute CallBack Function Success') res_dict['Result'].append(res) logger.info('Send JSON:%s' % (str(res_dict))) client.send(alphabet.str2hexbytes(json.dumps(res_dict, ensure_ascii=False))) if cmd_num > 0: mydb.close()
def tmp_echo(write_file, read_file): time.sleep(0.1) #print ('start echo thread') tmp_w_pipe = os.open(write_file, os.O_SYNC | os.O_CREAT | os.O_RDWR) os.write(tmp_w_pipe, alphabet.str2hexbytes(echo_str)) #print ('echo write echo done') tmp_r_pipe = os.open(read_file, os.O_RDONLY) #print ('echo open read_file done') while True: buf = alphabet.hexbytes2str(os.read(tmp_r_pipe, 32)) if buf == echo_str: return 0 time.sleep(1)
def heartbeat(logger): tcp_client = tcp_connect() mysql_cmd = {'CmdNum': 1, 'Linear': True, 'Cmd': ['heartbeat']} mutexFlag = mutex.acquire(False) if mutexFlag: tcp_client.send( alphabet.str2hexbytes(json.dumps(mysql_cmd, ensure_ascii=False))) res = tcp_recv(tcp_client, logger) if res != None: recv_json = json.loads(alphabet.hexbytes2str(res)) logger.info('Heart Beat Echo Recv %s' % (str(recv_json))) else: logger.info('Heart Beat mutex acquire Fail, push to next period') mutex.release() timer_sched(heartbeat, argv=(logger, )) tcp_client.close()
def create(self): self.role = 'server' write_pipe_file = '/var/tmp/write_pipe_%s' % (self.pipe_name) read_pipe_file = '/var/tmp/read_pipe_%s' % (self.pipe_name) if os.path.exists(write_pipe_file): print('pipe[%s] exists' % (write_pipe_file)) os.remove(write_pipe_file) if os.path.exists(read_pipe_file): print('pipe[%s] exists' % (read_pipe_file)) os.remove(read_pipe_file) os.mkfifo(read_pipe_file) os.mkfifo(write_pipe_file) #print ('server start open write_pipe[%s]'%(write_pipe_file)) self.write_pipe = os.open(write_pipe_file, os.O_SYNC | os.O_CREAT | os.O_RDWR) def tmp_echo(write_file, read_file): time.sleep(0.1) #print ('start echo thread') tmp_w_pipe = os.open(write_file, os.O_SYNC | os.O_CREAT | os.O_RDWR) os.write(tmp_w_pipe, alphabet.str2hexbytes(echo_str)) #print ('echo write echo done') tmp_r_pipe = os.open(read_file, os.O_RDONLY) #print ('echo open read_file done') while True: buf = alphabet.hexbytes2str(os.read(tmp_r_pipe, 32)) if buf == echo_str: return 0 time.sleep(1) tmp = threading.Thread(target=tmp_echo, args=(read_pipe_file, write_pipe_file)) tmp.start() #print ('server start open read_pipe[%s]'%(read_pipe_file)) self.read_pipe = os.open(read_pipe_file, os.O_RDONLY) #print ('server open pipe done') while True: buf = alphabet.hexbytes2str(os.read(self.read_pipe, 32)) if buf == echo_str: self.write(echo_str) break time.sleep(1) tmp.join() print('pipe[%s] create done...' % (self.pipe_name)) return 0
def esp8266_server_recv_process(client, recv_data, logger): global esp8266_global_dict, heart_beat_cnt recv_str = alphabet.hexbytes2str(recv_data) recv_str = recv_str.replace("'", '"') if len(recv_str) > 8: recv_dict = json.loads(recv_str) #logger.info('Recv JSON: %s'%(str(recv_dict))) name = recv_dict['name'] if recv_dict['ctrl'] == 'Register': find_flag = False for list_index in esp8266_global_list: list_index.update({'name': name, 'socket': client}) logger.info('Update, %s' % (name)) find_flag = True if not find_flag: esp8266_global_list.append({'name': name, 'socket': client}) logger.info('New, %s' % (name)) elif recv_dict['ctrl'] == 'HearBeat': heart_beat_cnt += 1 if heart_beat_cnt >= 500: heart_beat_cnt = 0 logger.info('Heart beat got 500 times, clear') pass
def test_proc(client, recv_data, a, b): print ('recv_data:[%s] args:[%s %d]'%(alphabet_pro.hexbytes2str(recv_data), a, b))
import sys sys.path.append('./alphabet') import alphabet_pro as alphabet if __name__ == "__main__": print( str(alphabet.hexstr2int('0123')) + ' <-> ' + alphabet.int2hexstr(291)) print( alphabet.hexstr2asciistr('30313233') + ' <-> ' + alphabet.asciistr2hexstr('0123')) print( str(alphabet.str2hexbytes('123')) + ' <-> ' + alphabet.hexbytes2str(b'\x31\x32\x33')) print( str(alphabet.hexstr2bytes('123abc')) + ' <-> ' + alphabet.bytes2hexstr(b'\x12\x3a\xbc'))
def read(self, bufsize): read_data = os.read(self.read_pipe, bufsize) return alphabet.hexbytes2str(read_data)