-
Notifications
You must be signed in to change notification settings - Fork 2
/
portutil.py
44 lines (37 loc) · 1.47 KB
/
portutil.py
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
import random
import subprocess
import pickle
def get_unused_port(name):
port_suggestion = 0
netstat_output = ':0'
while f"{':'}{port_suggestion}" in str(netstat_output):
port_suggestion = random.randint(1000, 60000)
netstat_output, _ = subprocess.Popen(['netstat', '-lat'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()
return port_suggestion
def print_ports():
response = []
try:
portdict = pickle.load(open("portdict.pickle", "rb"))
except (OSError, IOError) as e:
print('portdict.pickle does not exist')
for key, value in portdict.items():
response += [f' Using port {value} for service {key}']
print('\n'.join(response))
def print_swagger_paths():
response = []
try:
portdict = pickle.load(open("portdict.pickle", "rb"))
except (OSError, IOError) as e:
print('portdict.pickle does not exist')
for key, value in portdict.items():
response += [f' Swagger doc on http://127.0.0.1:{value}/swagger-ui for service {key}']
print('\n'.join(response))
def fill_portdict():
print('Filling portdict and starting services ...')
portdict = {}
for api in ['text-api', 'asr-api', 'ocr-api', 'meme-model-api', 'target-api', 'meme-detection-api']:
port = get_unused_port(api)
portdict[api] = port
pickle.dump(portdict, open("portdict.pickle", "wb"))