def Close(self): try: self.quoteClient.shutdown(socket.SHUT_RDWR) # 关闭行情接受客户端 except Exception: pass # 数据头 GReqHead = Trans.ReqHead() GReqHead.branch_id = self.serverInfo.branch_id GReqHead.exch_code = '8002' GReqHead.msg_flag = '1' GReqHead.msg_type = '1' GReqHead.term_type = '03' GReqHead.user_id = self.serverInfo.user_id GReqHead.user_type = '2' # 数据体 v_reqMag = Trans.ReqT8002() v_reqMag.oper_flag = '1' v_reqMag.user_id = self.serverInfo.user_id v_reqMag.user_type = '2' v_sMsg = GReqHead.toString() + v_reqMag.toString() ip = self.serverInfo.trans_ip port = self.serverInfo.trans_port client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((ip, port)) self.__SendGoldMsg(client, v_sMsg) close_str = self.__RecvGoldMsg(client) client.close() closeR = re.findall(r'rsp_msg=(.*?)#', close_str, re.M | re.I)[0] if closeR == '处理成功': print('关闭成功') return (True, closeR) print('关闭失败') return (False, closeR)
def getCustInfo(self): # 数据头 GReqHead = Trans.ReqHead() GReqHead.branch_id = self.serverInfo.branch_id GReqHead.exch_code = '1020' GReqHead.msg_flag = '1' GReqHead.msg_type = '1' GReqHead.term_type = '03' GReqHead.user_id = self.serverInfo.user_id GReqHead.user_type = '2' # 数据体 v_reqMag = Trans.ReqT1020() v_reqMag.acct_no = self.serverInfo.user_id v_reqMag.is_check_stat = '1' v_reqMag.oper_flag = '1' v_reqMag.oper_flag = '1' v_reqMag.qry_cust_info = '1' v_reqMag.qry_defer = '1' v_reqMag.qry_forward = '1' v_reqMag.qry_fund = '1' v_reqMag.qry_storage = '1' v_reqMag.qry_surplus = '1' v_sMsg = GReqHead.toString() + v_reqMag.toString() ip = self.serverInfo.trans_ip port = self.serverInfo.trans_port client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((ip, port)) self.__SendGoldMsg(client, v_sMsg) customInfo_str = self.__RecvGoldMsg(client) client.close() self.custInfo.fromString(customInfo_str) return self.custInfo.toDict()
def getTradeInfo(self): # 数据头 GReqHead = Trans.ReqHead() GReqHead.branch_id = self.serverInfo.branch_id GReqHead.exch_code = '6002' GReqHead.msg_flag = '1' GReqHead.msg_type = '1' GReqHead.term_type = '03' GReqHead.user_id = self.serverInfo.user_id GReqHead.user_type = '2' # 数据体 v_reqMag = Trans.ReqT6002() v_reqMag.alm_view_field = '∧'.join( list(vars(Trans.Alm_View_item()).keys())) # v_reqMag.alm_view_field="exch_date∧order_no∧market_id∧prod_code∧exch_code∧entr_price∧entr_amount∧remain_amount∧offset_flag∧entr_stat∧e_term_type∧e_exch_time∧c_term_type∧c_exch_time∧rsp_msg∧local_order_no∧" v_reqMag.curr_page = '1' v_reqMag.login_branch_id = self.serverInfo.branch_id v_reqMag.login_teller_id = self.serverInfo.user_id v_reqMag.oper_flag = '1' v_reqMag.paginal_num = '500' v_reqMag.query_id = 'AcctEntrFlow' v_sMsg = GReqHead.toString() + v_reqMag.toString() # 'cb8e8527160021032 1021805322 B00151853 #alm_view_field=exch_date∧order_no∧market_id∧prod_code∧exch_code∧entr_price∧entr_amount∧remain_amount∧offset_flag∧entr_stat∧e_term_type∧e_exch_time∧c_term_type∧c_exch_time∧rsp_msg∧local_order_no∧#curr_page=1#login_branch_id=B00151853#login_teller_id=1021805322#oper_flag=1#paginal_num=500#query_id=AcctEntrFlow#prod_code=#exch_code=#b_offset_flag=#' ip = self.serverInfo.query_ip port = self.serverInfo.query_port client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((ip, port)) self.__SendGoldMsg(client, v_sMsg) tradeInfo_str = self.__RecvGoldMsg(client) client.close() alm_view = Trans.Alm_View() alm_view.fromString(tradeInfo_str) return alm_view.toDict()
def __init__(self): self.serverInfo = Trans.ServerInfo() self.custInfo = Trans.CustomerInfo() self.user_id = '' self.__user_pwd = '' self.quote = Trans.Quote() socket.setdefaulttimeout(2) self.quoteClient = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def login(self, username, password): # 数据头 GReqHead = Trans.ReqHead() GReqHead.exch_code = '8006' GReqHead.msg_type = '1' GReqHead.msg_flag = '1' GReqHead.term_type = '03' GReqHead.user_type = '2' GReqHead.user_id = username self.serverInfo.user_id = username # 数据体 v_reqMag = Trans.ReqT8006() v_reqMag.bank_no = Constant['bank_no'] v_reqMag.login_ip = Constant['login_ip'] v_reqMag.net_agent = '1' v_reqMag.net_envionment = '2' v_reqMag.oper_flag = '1' v_reqMag.user_id = username v_reqMag.user_id_type = '1' v_reqMag.user_pwd = Comm.MD5(password) v_reqMag.user_type = '2' # 数据头+数据体 v_sMsg = GReqHead.toString() + v_reqMag.toString() self.user_id = v_reqMag.user_id self.__user_pwd = v_reqMag.user_pwd publicKey = Comm.getCrtFilePublickey("./api/cert/server.crt") buffer = ''.encode('utf-8') for i in range(0, len(v_sMsg), 100): buffer = buffer + Comm.rsaEncrypt( v_sMsg[i:i + 100].encode('utf-8'), publicKey) buffer_Len_Str = Comm.Fill(str(len(buffer)), '0', 8, 'L') client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) log_ip = Constant['log_server_ip'] log_port = Constant['log_server_port'] client.connect((log_ip, log_port)) client.send(buffer_Len_Str.encode('utf-8')) client.send(buffer) buffer4 = client.recv(8) buffer5 = client.recv(int(buffer4.decode('utf-8'))) client.close() privateKey = Comm.getPfxFilePrivatekey("./api/cert/client.pfx", '123456') streamArr = ''.encode('utf-8') for i in range(0, len(buffer5), 128): buffer6 = buffer5[i:i + 128] streamArr = streamArr + Comm.rsaDecrypt(buffer6, privateKey) logSevStr = streamArr.decode('gbk') self.serverInfo.fromString(logSevStr) if self.serverInfo.rsp_msg == '处理成功': return True, "登陆成功" else: return False, self.serverInfo.rsp_msg
def trade(self, code, bs, price, amount): ''' 交易方向 4041(开多) 4042(开空) 4043(平多) 4044(平空) ''' if code not in CodeList: return False, "交易对象" + code + "不在列表中" if bs == 'ob': exch_code = '4041' v_reqMag = Trans.ReqT4041() elif bs == 'os': exch_code = '4042' v_reqMag = Trans.ReqT4042() elif bs == 'cb': exch_code = '4043' v_reqMag = Trans.ReqT4043() elif bs == 'cs': exch_code = '4044' v_reqMag = Trans.ReqT4044() else: return False, "交易方向错误,应为ob(开多),os(开空),cb(平多),cs(平空)中的一种" # 数据头 GReqHead = Trans.ReqHead() GReqHead.branch_id = self.serverInfo.branch_id GReqHead.exch_code = exch_code GReqHead.msg_flag = '1' GReqHead.msg_type = '1' GReqHead.term_type = '03' GReqHead.user_id = self.serverInfo.user_id GReqHead.user_type = '2' # 数据体 v_reqMag.acct_no = self.serverInfo.user_id v_reqMag.client_serial_no = self.serverInfo.user_id + str( (datetime.datetime.now().hour * 3600 + datetime.datetime.now().minute * 60 + datetime.datetime.now().second) * 10) # '1021805322584010' v_reqMag.cust_id = self.serverInfo.user_id v_reqMag.entr_amount = amount # 交易数量 v_reqMag.entr_price = price # 交易价格 v_reqMag.prod_code = code # 交易品种 v_sMsg = GReqHead.toString() + v_reqMag.toString() ip = self.serverInfo.trans_ip port = self.serverInfo.trans_port client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((ip, port)) self.__SendGoldMsg(client, v_sMsg) trade_Dict = Comm.splitInfoStr(self.__RecvGoldMsg(client)) print(trade_Dict) client.close()
def getQuote_start(self): # 数据体 v_reqMag = Trans.GBcMsgReqLink() v_reqMag.RspCode = '' v_reqMag.RspMsg = '' v_reqMag.again_flag = '0' v_reqMag.branch_id = self.serverInfo.branch_id v_reqMag.cust_type_id = 'C01' v_reqMag.is_lfv = '1' v_reqMag.lan_ip = Constant['login_ip'] v_reqMag.term_type = '' v_reqMag.user_id = self.user_id v_reqMag.user_key = datetime.datetime.now().strftime('%H%M%S%f')[0:-3] v_reqMag.user_pwd = self.__user_pwd v_reqMag.user_type = Constant['user_type'] v_reqMag.www_ip = '' v_sMsg = v_reqMag.toString() ip = self.serverInfo.broadcast_ip port = self.serverInfo.broadcast_port #client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: self.quoteClient.shutdown((socket.SHUT_RDWR)) except: pass self.quoteClient = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client = self.quoteClient client.connect((ip, port)) self.__SendGoldMsg(client, v_sMsg) for i in range(48): #?????????????到超时结束,行情表弟有时会有改变,怎么确定? self.getQuote_step()
def getQuote(self): # 数据体 v_reqMag = Trans.GBcMsgReqLink() v_reqMag.RspCode = '' v_reqMag.RspMsg = '' v_reqMag.again_flag = '0' v_reqMag.branch_id = self.serverInfo.branch_id v_reqMag.cust_type_id = 'C01' v_reqMag.is_lfv = '1' v_reqMag.lan_ip = Constant['login_ip'] v_reqMag.term_type = '' v_reqMag.user_id = self.user_id v_reqMag.user_key = datetime.datetime.now().strftime('%H%M%S%f')[0:-3] v_reqMag.user_pwd = self.__user_pwd v_reqMag.user_type = Constant['user_type'] v_reqMag.www_ip = '' v_sMsg = v_reqMag.toString() ip = self.serverInfo.broadcast_ip port = self.serverInfo.broadcast_port client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((ip, port)) self.__SendGoldMsg(client, v_sMsg) for i in range(52): QuoteInfo_Dict = self.__RecvGoldMsg(client) self.quote.fromDict(QuoteInfo_Dict) client.close()
def cancel(self, order_no): # 数据头 GReqHead = Trans.ReqHead() GReqHead.branch_id = self.serverInfo.branch_id GReqHead.exch_code = '4061' GReqHead.msg_flag = '1' GReqHead.msg_type = '1' GReqHead.term_type = '03' GReqHead.user_id = self.serverInfo.user_id GReqHead.user_type = '2' # 数据体 v_reqMag = Trans.ReqT4061() v_reqMag.cancel_order_no = order_no v_reqMag.oper_flag = 1 v_sMsg = GReqHead.toString() + v_reqMag.toString() ip = self.serverInfo.trans_ip port = self.serverInfo.trans_port client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((ip, port)) self.__SendGoldMsg(client, v_sMsg) trade_Dict = Comm.splitInfoStr(self.__RecvGoldMsg(client)) print(trade_Dict) client.close()