-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
43 lines (38 loc) · 1.16 KB
/
helper.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
import socket
import os
def create_dir_if_missing(path:str):
if not os.path.exists(path):
os.makedirs(path)
def get_my_ip_address(remote_server="google.com"):
"""
Return the/a network-facing IP number for this system.
"""
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect((remote_server, 80))
return s.getsockname()[0]
def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
def get_flask_port_when_up(path:str) -> int:
"""
waits for flask to be up then return port
"""
import time
while not os.path.exists(path):
print("flask config missing")
time.sleep(0.5)
pass
while True:
import json
import requests
line='{"port":-1}'
with open(path,"r") as f:
line=f.readline()
port=json.loads(line)["port"]
if is_port_in_use(port):
try:
requests.get("http://localhost:"+str(port)+"/")
return port
except:
continue
time.sleep(0.5)