mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-15 20:57:56 +08:00
* WIP * import find_available_port method * move method to utils so I can add to eventually add to gui * add fixed-port flag to tests * Update server/utils/utils.py Co-Authored-By: Tony Tung <tonytung@merly.org> * pr review suggestions * pr review suggestions * fix outdated package.json * update error message * simplify find_available_port function * Auto scan for ports unless port is specified. * fix tests * fix comment for find_available_port * lint error * differentiate port error from generic os error * add errno to OSerror * pr review fixes * raise e -> raise * oserror -> socket error
20 lines
712 B
Python
20 lines
712 B
Python
import contextlib
|
|
import errno
|
|
import socket
|
|
|
|
|
|
def find_available_port(host, port=5005):
|
|
"""
|
|
Helper method to find open port on host. Tries 5000 ports incremented from the specified port
|
|
"""
|
|
# Takes approx 2 seconds to do a scan of 5000 ports on my laptop
|
|
num_ports_to_try = 5000
|
|
for port_to_try in range(port, port + num_ports_to_try):
|
|
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|
|
try:
|
|
s.bind((host, port_to_try))
|
|
return port_to_try
|
|
except socket.error:
|
|
pass
|
|
raise socket.error(errno.EADDRINUSE, f"No port in range {port} - {port + num_ports_to_try - 1} available.")
|