https://github.com/rainx/pytdx/blob/master/pytdx/util/best_ip.py
可以设置全局超时
import socket
import time
socket.setdefaulttimeout(20)
导入库
from pytdx.hq import TdxHq_API
连接标准行情接口
standard_api = TdxHq_API(auto_retry=True, raise_exception=False)
while True:
try:
is_tdx_standard_connect = standard_api.connect('119.147.212.81', 7709, time_out=30)
except Exception as e:
print('time out to connect to pytdx')
time.sleep(3)
continue
if is_tdx_standard_connect is not False:# 失败了返回False,成功了返回地址
print('connect to pytdx standard api successful')
break
else:
time.sleep(3)
continue
pass
导入库
from pytdx.exhq import TdxExHq_API, TDXParams
连接扩展行情接口
ex_api = TdxExHq_API(auto_retry=True, raise_exception=False)
while True:
try:
is_tdx_ex_connect = ex_api.connect('106.14.95.149', 7727, time_out=30)
except Exception as e:
print('time out to connect to pytdx')
print(e)
time.sleep(3)
continue
if is_tdx_ex_connect is not False:# 失败了返回False,成功了返回地址
print('connect to pytdx extend api successful')
break
else:
time.sleep(3)
continue
pass