This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurator.py
66 lines (56 loc) · 2.11 KB
/
configurator.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
import subprocess
import re
import os
from sys import platform
class Configurator:
"""
An abstract base class to config Resolver and Server settings.
A note for using this. Do not create an object from this class. Use the class directly.
For example, Configurator.SERVER_IP to access the SERVER_IP attribute.
"""
IP = ""
TCP_PORT = None
UDP_PORT = None
OTHERS = []
BUFFER_SIZE = 4096
@staticmethod
def get_ip() -> str:
"""Extract ip from the local machine."""
if platform in ["linux", "linux2"]:
ifconfig_result = str(subprocess.check_output(["ifconfig", "eth0"]))
match = re.search(r"inet \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}", ifconfig_result)[
0
]
return match.split(" ")[1]
elif platform == "win32":
ipconfig_result = str(subprocess.check_output(["ipconfig"]))
matches = re.findall(
r"(?<=IPv4 Address. . . . . . . . . . . : )\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}",
ipconfig_result,
)
print("Select an ip from this list:")
for i, ip in enumerate(matches):
print(f"({i}) {ip}")
return matches[int(input("Enter the index of the ip: "))]
@staticmethod
def config_me(udp_port: int, tcp_port: int):
"""Config socket's constants."""
print("##### SOCKET CONFIG #####")
Configurator.IP = Configurator.get_ip()
Configurator.TCP_PORT = tcp_port
Configurator.UDP_PORT = udp_port
os.system("clear")
@staticmethod
def config_others(num_partners: int):
"""Config others socket's constants."""
print("##### SOCKET CONFIG #####")
for _ in range(num_partners):
ip = input("IP: ")
udp_port = int(input("UDP port number: "))
tcp_port = int(input("TCP port number: "))
Configurator.OTHERS.append(dict(ip=ip, udp=udp_port, tcp=tcp_port))
os.system("clear")
@staticmethod
def set_buffer_size(size: int):
if size > 0:
Configurator.BUFFER_SIZE = size