-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
26 lines (22 loc) · 682 Bytes
/
config.py
File metadata and controls
26 lines (22 loc) · 682 Bytes
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
import os
import socket
class Config:
@staticmethod
def find_available_port():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 0))
port = s.getsockname()[1]
s.close()
return port
@staticmethod
def write_port_to_file(port):
with open('server_port.txt', 'w') as f:
f.write(str(port))
@staticmethod
def get_port_from_file():
with open('server_port.txt', 'r') as f:
return int(f.read().strip())
if __name__ == '__main__':
port = Config.find_available_port()
Config.write_port_to_file(port)
print(f"Port {port} written to server_port.txt")