-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_finder.py
35 lines (28 loc) · 1.02 KB
/
port_finder.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
import socket
from contextlib import closing
def is_port_available(port: int) -> bool:
# First try to connect to the port
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
try:
# Try connecting to the port
result = sock.connect_ex(("127.0.0.1", port))
if result == 0: # Can connect = port is in use
return False
except OSError:
pass # Connection failed, which is what we want
# Then try to bind to the port
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("127.0.0.1", port))
return True
except OSError:
return False
def find_next_free_port(start_port: int = 8000) -> int:
port = start_port
while not is_port_available(port):
port += 1
return port
if __name__ == "__main__":
free_port = find_next_free_port(8501)
print(free_port)