Add REUSE flag to socket when scanning for unused ports.

This commit is contained in:
grossmj
2014-11-01 15:44:18 -06:00
parent 89e80fd74b
commit 4d23c5917c
2 changed files with 5 additions and 3 deletions

View File

@ -58,9 +58,11 @@ def find_unused_port(start_port, end_port, host='127.0.0.1', socket_type="TCP",
if ":" in host:
# IPv6 address support
with socket.socket(socket.AF_INET6, socket_type) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port)) # the port is available if bind is a success
else:
with socket.socket(socket.AF_INET, socket_type) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port)) # the port is available if bind is a success
return port
except OSError as e: