mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-19 07:53:00 +00:00
28 lines
637 B
Python
28 lines
637 B
Python
"""Miscellaneous utilities"""
|
|
|
|
# Copyright (c) IPython Development Team.
|
|
# Distributed under the terms of the Modified BSD License.
|
|
|
|
import socket
|
|
import time
|
|
|
|
def random_port():
|
|
"""get a single random port"""
|
|
sock = socket.socket()
|
|
sock.bind(('', 0))
|
|
port = sock.getsockname()[1]
|
|
sock.close()
|
|
return port
|
|
|
|
def wait_for_server(ip, port, timeout=10):
|
|
"""wait for a server to show up at ip:port"""
|
|
tic = time.time()
|
|
while time.time() - tic < timeout:
|
|
try:
|
|
socket.create_connection((ip, port))
|
|
except socket.error:
|
|
time.sleep(0.1)
|
|
else:
|
|
break
|
|
|