def api_authentication(auth_url): try: # ----- auth step 1 _header = AUTH_HEADER _header['Host'] = auth_url[7:] _timestr = _time() _url = auth_url + '/?%s' % _timestr r = requests.post(_url, data=AUTH_DATA, headers=_header) tmp_data = r.text.split('\r\n\r\n') yh_api_logger.debug(tmp_data[0]) yh_api_logger.debug(tmp_data[1]) # ----- auth step 2 _header2 = AUTH_HEADER _header2['Host'] = auth_step_2_host_post _url2 = 'http://' + auth_step_2_host_post + '/?%s' % _timestr r2 = requests.post(_url2, data=AUTH_DATA_2, headers=_header2) tmp_data2 = r2.text.split('\r\n\r\n') yh_api_logger.debug(tmp_data2[0]) yh_api_logger.debug(tmp_data2[1]) tmp_dict = literal_eval(tmp_data2[1]) connection_session = int(tmp_dict['ConnectionSession']) yh_api_logger.info('the ConnectionSession code is : %s' %connection_session) except BaseException, e: yh_api_logger.error(traceback.format_exc()) connection_session = ''
def rundb(sql, vals=[], db_conf={}, result=True): try: curs, conn = connectdb() curs.execute(sql, vals) conn.commit() r = None if result: r = curs.fetchall() return r except BaseException, e: yh_api_logger.error(traceback.format_exc()) yh_api_logger.error(sql) r = None
def get_auth_url(): # ----- get ip port try: url_ip_port = 'http://wxhq.yhqh.com.cn' res_ip_port = requests.get(url=url_ip_port) cont_ip_port = res_ip_port.text if 'AuthAddress' in cont_ip_port: idx = cont_ip_port.find('AuthAddress') _cont = cont_ip_port[idx: idx+100] tmpip, tmpport = _cont.split('\r\n')[:2] tmpip = tmpip.split(',')[0].split(':')[1].strip()[1:-1] tmpport = int(tmpport.split(',')[0].split(':')[1].strip()) auth_url = 'http://%s:%s' % (tmpip, tmpport) except BaseException, e: yh_api_logger.error(traceback.format_exc()) auth_url = ''
def api_price(connection_session): try: _header = AUTH_HEADER _header['Host'] = auth_step_2_host_post _timestr = _time() _url = 'http://' + auth_step_2_host_post + '/?%s' % _timestr r = requests.post(_url, data=(PRICE_DATA % connection_session), headers=_header) tmp_data = r.text.split('\r\n\r\n') yh_api_logger.debug(tmp_data[0]) yh_api_logger.debug(tmp_data[1]) tmp_dict = literal_eval(tmp_data[1]) if 'ErrMsg' in tmp_dict: if tmp_dict['ErrMsg'] == '你需要登录': yh_api_logger.error(tmp_dict) except BaseException, e: yh_api_logger.error(traceback.format_exc())
def api_trans_auth_session(): try: # ----- auth step 1 _header = AUTH_HEADER _header['Host'] = TRANS_HOST _timestr = _time() _time_trans_str = _time_trans() _url = TRANS_URL % _timestr _d = quote_plus(TRANS_AUTH_DATA_SP1) trans_data = TRANS_AUTH_DATA_ALL % (_d, _time_trans_str) r = requests.post(_url, data=trans_data, headers=_header) tmp_data = literal_eval(r.text) print tmp_data trans_session = int(tmp_data['Session']) yh_api_logger.debug(tmp_data) except BaseException, e: yh_api_logger.error(traceback.format_exc()) trans_session = None
def api_trans_auth_check(trans_session): try: # ----- auth step 2 check_res = False _header = AUTH_HEADER _header['Host'] = TRANS_HOST _timestr = _time() _time_trans_str = _time_trans() _url = TRANS_URL % _timestr _d2 = quote_plus(TRANS_AUTH_DATA_SP2 % trans_session) trans_data = TRANS_AUTH_DATA_ALL % (_d2, _time_trans_str) r = requests.post(_url, data=trans_data, headers=_header) tmp_data2 = r.text print tmp_data2 if 'error_no' not in tmp_data2: check_res = True yh_api_logger.debug(tmp_data2) except BaseException, e: yh_api_logger.error(traceback.format_exc())
def trans_short_close(trans_session, code, ifcode, entrust_amount=1, eo_flag=1): try: _header = AUTH_HEADER _header['Host'] = TRANS_HOST _timestr = _time() _time_trans_str = _time_trans() _url = TRANS_URL % _timestr res_price = price_times(code) buy_price = res_price['BuyPrice0'] _d = quote_plus(LONG_SHORT_DATA % (trans_session, ifcode, buy_price, entrust_amount, 'S', eo_flag)) trans_data = TRANS_AUTH_DATA_ALL % (_d, _time_trans_str) r = requests.post(_url, data=trans_data, headers=_header) tmp_data = r.text yh_api_logger.debug(tmp_data) except BaseException, e: tmp_data = 'error_info: %s' % e yh_api_logger.error(traceback.format_exc())
def api_price_second(code, connection_session): ''' return : Market 3003:代表中金所 code、TradeCode:合约code name:合约名字 now:最新价格 open:开盘价 high:最高价 low:最低价 avg:均价 Settle:结算价 CurVolume:现手 BuyPrice0:买价 BuyVol0:买量 SellPrice0:卖价 SellVol0:卖量 Hold:持仓 ''' try: _header = AUTH_HEADER _header['Host'] = auth_step_2_host_post _timestr = _time() _url = 'http://' + auth_step_2_host_post + '/?%s' % _timestr r = requests.post(_url, data=(PRICE_SECOND_DATA % (connection_session, code)), headers=_header) tmp_data = r.text.split('\r\n\r\n') yh_api_logger.debug(tmp_data[0]) yh_api_logger.debug(tmp_data[1]) tmp_dict = literal_eval(tmp_data[1]) if 'ErrMsg' in tmp_dict: if tmp_dict['ErrMsg'] == '你需要登录': yh_api_logger.error('%s: %s \n 需要登录', code, tmp_data) elif 'Symbol' in tmp_dict: _d = tmp_dict['Symbol'][0] yh_api_logger.info(_d) return _d except BaseException, e: yh_api_logger.error('%s : %s', code, traceback.format_exc())
def api_auth_connection_session(connection_session): try: # ----- connection_session step 1 _timestr = _time() _header = AUTH_HEADER _header['Host'] = auth_step_2_host_post _url = 'http://' + auth_step_2_host_post + '/?%s' % _timestr r = requests.post(_url, data=CONN_SESS_DATA % connection_session, headers=_header) tmp_data = r.text.split('\r\n\r\n') yh_api_logger.debug(tmp_data[0]) yh_api_logger.debug(tmp_data[1]) # ------ connection_session step 2 r2 = requests.post(_url, data=CONN_SESS_DATA_2 % connection_session, headers=_header) tmp_data2 = r2.text.split('\r\n\r\n') yh_api_logger.debug(tmp_data2[0]) yh_api_logger.debug(tmp_data2[1]) tmp_dict = literal_eval(tmp_data2[1]) except BaseException, e: yh_api_logger.error(traceback.format_exc())
def disconnectdb(_curs, _conn): try: _curs.close() except BaseException, e: yh_api_logger.error(traceback.format_exc())
#连接mysql的方法:connect('ip','user','password','dbname') conn = mdb.connect(_host, _uid, _pwd, _db, charset=_charcode) curs = conn.cursor() return curs, conn def disconnectdb(_curs, _conn): try: _curs.close() except BaseException, e: yh_api_logger.error(traceback.format_exc()) try: _conn.close() except BaseException, e: yh_api_logger.error(traceback.format_exc()) def rundb(sql, vals=[], db_conf={}, result=True): try: curs, conn = connectdb() curs.execute(sql, vals) conn.commit() r = None if result: r = curs.fetchall() return r except BaseException, e: yh_api_logger.error(traceback.format_exc()) yh_api_logger.error(sql) r = None