Repository navigation
Expand file tree
/
Copy pathxtconn.py
More file actions
106 lines (79 loc) · 2.31 KB
/
Copy pathxtconn.py
File metadata and controls
106 lines (79 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#coding:utf-8
import os
import json
from .xtdatacenter import try_create_client
from . import xtbson as bson
### config
localhost = '127.0.0.1'
### function
def try_create_connection(addr):
'''
addr: 'localhost:58610'
'''
ip, port = addr.split(':')
if not ip:
ip = localhost
if not port:
raise Exception('invalid port')
cl = try_create_client()
cl.set_config_addr(addr)
from .xtdata import status_callback
cl.subscribeCommonControl("watchxtquantstatus", bson.BSON.encode({}), status_callback)
ec, msg = cl.connect()
if ec < 0:
raise Exception((ec, msg))
return cl
def create_connection(addr):
try:
return try_create_connection(addr)
except Exception as e:
return None
def scan_available_server():
result = []
try:
from .xtdatacenter import get_local_server_port
local_server_port = get_local_server_port()
if local_server_port:
result.append(f'127.0.0.1:{local_server_port}')
except:
pass
try:
config_dir = os.path.abspath(os.path.join(os.environ['USERPROFILE'], '.xtquant'))
for f in os.scandir(config_dir):
full_path = f.path
is_running = False
try:
os.remove(os.path.join(full_path, 'running_status'))
except PermissionError:
is_running = True
except Exception as e:
pass
if not is_running:
continue
try:
config = json.load(open(os.path.join(full_path, 'xtdata.cfg'), 'r', encoding = 'utf-8'))
ip = config.get('ip', localhost)
port = config.get('port', None)
if not port:
raise Exception(f'invalid port: {port}')
addr = f'{ip}:{port}'
result.append(addr)
except Exception as e:
continue
except Exception as e:
pass
result.sort()
return result
def connect_any(addr_list):
'''
addr_list: [ addr, ... ]
addr: 'localhost:58610'
'''
for addr in addr_list:
try:
cl = create_connection(addr)
if cl:
return cl
except Exception as e:
continue
return None