-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
88 lines (70 loc) · 2.08 KB
/
utils.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
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
import sys
import socket
def check_listening(host, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
except socket.error:
return False
else:
sock.close()
return True
def tcp_connect(host, port, blocking=True):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
sock.setblocking(blocking)
return sock
def tcp_listen(host, port, blocking=True, max_conn=1000):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(max_conn)
sock.setblocking(blocking)
return sock
def check_udp_port_available(port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('127.0.0.1', port))
except socket.error:
return False
else:
sock.close()
return True
def check_python_version(main, sub):
if sys.version_info < (main, sub):
raise RuntimeError(f'"Python {main}.{sub}" or higher version is required.')
return True
def stringify_bytes_val(val):
unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
level = 0
positive = True
if val < 0:
val = - val
positive = False
while level < len(unit) - 1:
if val < 1000:
break
else:
level += 1
val /= 1000
return f'{"" if positive else "-"}{str(val)[:5]} {unit[level]}'
def stringify_speed_val(val):
return stringify_bytes_val(val) + '/s'
def validate_id_string(id_str, length):
try:
if not id_str or len(id_str) != length:
raise ValueError
int(id_str, 16) # check if `id_str` is a hex format string or not.
return True
except ValueError:
return False
def validate_ip_address(ip_str):
try:
values = ip_str.split('.')
if len(values) != 4:
raise ValueError
for v in values:
int(v)
return True
except ValueError:
return False