diff --git a/jupyterhub/app.py b/jupyterhub/app.py index b8c0a1f0..bcecdae0 100644 --- a/jupyterhub/app.py +++ b/jupyterhub/app.py @@ -50,7 +50,7 @@ from ._data import DATA_FILES_PATH from .log import CoroutineLogFormatter, log_request from .traitlets import URLPrefix, Command from .utils import ( - url_path_join, localhost, + url_path_join, ISO8601_ms, ISO8601_s, ) # classes for config @@ -260,7 +260,7 @@ class JupyterHub(Application): token = orm.new_token() return token - proxy_api_ip = Unicode(localhost(), config=True, + proxy_api_ip = Unicode('127.0.0.1', config=True, help="The ip for the proxy API handlers" ) proxy_api_port = Integer(config=True, @@ -272,7 +272,7 @@ class JupyterHub(Application): hub_port = Integer(8081, config=True, help="The port for this process" ) - hub_ip = Unicode(localhost(), config=True, + hub_ip = Unicode('127.0.0.1', config=True, help="The ip for this process" ) diff --git a/jupyterhub/orm.py b/jupyterhub/orm.py index 0966414f..b296d619 100644 --- a/jupyterhub/orm.py +++ b/jupyterhub/orm.py @@ -26,7 +26,7 @@ from sqlalchemy import create_engine from .utils import ( random_port, url_path_join, wait_for_server, wait_for_http_server, - new_token, hash_token, compare_token, localhost, + new_token, hash_token, compare_token, ) @@ -78,7 +78,7 @@ class Server(Base): ip = self.ip if ip in {'', '0.0.0.0'}: # when listening on all interfaces, connect to localhost - ip = localhost() + ip = '127.0.0.1' return "{proto}://{ip}:{port}".format( proto=self.proto, ip=ip, @@ -100,7 +100,7 @@ class Server(Base): since it can be non-connectable value, such as '', meaning all interfaces. """ if self.ip in {'', '0.0.0.0'}: - return self.url.replace('localhost', self.ip or '*', 1) + return self.url.replace('127.0.0.1', self.ip or '*', 1) return self.url @gen.coroutine @@ -109,12 +109,12 @@ class Server(Base): if http: yield wait_for_http_server(self.url, timeout=timeout) else: - yield wait_for_server(self.ip or localhost(), self.port, timeout=timeout) + yield wait_for_server(self.ip or '127.0.0.1', self.port, timeout=timeout) def is_up(self): """Is the server accepting connections?""" try: - socket.create_connection((self.ip or localhost(), self.port)) + socket.create_connection((self.ip or '127.0.0.1', self.port)) except socket.error as e: if e.errno == errno.ENETUNREACH: try: diff --git a/jupyterhub/spawner.py b/jupyterhub/spawner.py index 1da13f8f..14b5735a 100644 --- a/jupyterhub/spawner.py +++ b/jupyterhub/spawner.py @@ -22,7 +22,7 @@ from traitlets import ( ) from .traitlets import Command -from .utils import random_port, localhost +from .utils import random_port class Spawner(LoggingConfigurable): """Base class for spawning single-user notebook servers. @@ -41,7 +41,7 @@ class Spawner(LoggingConfigurable): hub = Any() authenticator = Any() api_token = Unicode() - ip = Unicode(localhost(), config=True, + ip = Unicode('127.0.0.1', config=True, help="The IP address (or hostname) the single-user server should listen on" ) start_timeout = Integer(60, config=True, diff --git a/jupyterhub/tests/mocking.py b/jupyterhub/tests/mocking.py index df4c9e66..d2d5e8bb 100644 --- a/jupyterhub/tests/mocking.py +++ b/jupyterhub/tests/mocking.py @@ -17,7 +17,6 @@ from ..spawner import LocalProcessSpawner from ..app import JupyterHub from ..auth import PAMAuthenticator from .. import orm -from ..utils import localhost from pamela import PAMError @@ -111,7 +110,7 @@ class MockHub(JupyterHub): db_file = None def _ip_default(self): - return localhost() + return '127.0.0.1' def _authenticator_class_default(self): return MockPAMAuthenticator diff --git a/jupyterhub/tests/test_orm.py b/jupyterhub/tests/test_orm.py index 1e296d55..983c4d12 100644 --- a/jupyterhub/tests/test_orm.py +++ b/jupyterhub/tests/test_orm.py @@ -20,7 +20,7 @@ def test_server(db): assert server.proto == 'http' assert isinstance(server.port, int) assert isinstance(server.cookie_name, str) - assert server.host == 'http://localhost:%i' % server.port + assert server.host == 'http://127.0.0.1:%i' % server.port assert server.url == server.host + '/' assert server.bind_url == 'http://*:%i/' % server.port server.ip = '127.0.0.1' diff --git a/jupyterhub/tests/test_spawner.py b/jupyterhub/tests/test_spawner.py index 89c94a70..69ed7a5a 100644 --- a/jupyterhub/tests/test_spawner.py +++ b/jupyterhub/tests/test_spawner.py @@ -45,7 +45,7 @@ def new_spawner(db, **kwargs): def test_spawner(db, io_loop): spawner = new_spawner(db) io_loop.run_sync(spawner.start) - assert spawner.user.server.ip == 'localhost' + assert spawner.user.server.ip == '127.0.0.1' # wait for the process to get to the while True: loop time.sleep(1) @@ -59,7 +59,7 @@ def test_spawner(db, io_loop): def test_single_user_spawner(db, io_loop): spawner = new_spawner(db, cmd=['jupyterhub-singleuser']) io_loop.run_sync(spawner.start) - assert spawner.user.server.ip == 'localhost' + assert spawner.user.server.ip == '127.0.0.1' # wait for http server to come up, # checking for early termination every 1s def wait(): diff --git a/jupyterhub/utils.py b/jupyterhub/utils.py index dcf01335..811e7812 100644 --- a/jupyterhub/utils.py +++ b/jupyterhub/utils.py @@ -195,35 +195,3 @@ def url_path_join(*pieces): return result -def localhost(): - """Return localhost or 127.0.0.1""" - if hasattr(localhost, '_localhost'): - return localhost._localhost - binder = connector = None - try: - binder = socket.socket() - binder.bind(('localhost', 0)) - binder.listen(1) - port = binder.getsockname()[1] - def accept(): - try: - conn, addr = binder.accept() - except ConnectionAbortedError: - pass - else: - conn.close() - t = Thread(target=accept) - t.start() - connector = socket.create_connection(('localhost', port), timeout=10) - t.join(timeout=10) - except (socket.error, socket.gaierror) as e: - warnings.warn("localhost doesn't appear to work, using 127.0.0.1\n%s" % e, RuntimeWarning) - localhost._localhost = '127.0.0.1' - else: - localhost._localhost = 'localhost' - finally: - if binder: - binder.close() - if connector: - connector.close() - return localhost._localhost