blob: 1bf790d06b5cb23279d597afa33a7cc545dc626d (
plain) (
blame)
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
|
import random
import socket
ATTEMPTS = 25
# range 10000-10199 is reserved for Skynet on Sandbox machines
MIN_PORT = 10200
MAX_PORT = 25000
def is_port_open(host, port):
_socket = socket.socket(socket.AF_INET)
return _socket.connect_ex((host, port)) != 0
def find_free_port(range_start=MIN_PORT, range_end=MAX_PORT, attempts=ATTEMPTS):
"""
Finds free port
:param range_start: start of range
:param range_end: end of range
:param attempts: number of tries to find free port
:return: some open port in a given range
"""
ports = [random.randint(range_start, range_end) for _ in range(attempts)]
while ports:
port = ports.pop()
if is_port_open('', port):
return port
raise RuntimeError('Could not find free port in range = ' + str((range_start, range_end)))
|