11import argparse
22import logging
33import os
4+ from urllib .parse import urlparse
45
56from tornado import ioloop
67from tornado .httpserver import HTTPServer
78from tornado .log import app_log as log
89from tornado .log import enable_pretty_logging , gen_log
910
1011from .activity import start_activity_update
11- from .proxy import configure_ssl , make_proxy_app , get_port_from_env
12+ from .proxy import configure_ssl , make_proxy_app
13+
14+
15+ def _default_address_and_port () -> tuple [str , int ]:
16+ """
17+ Get the Address and Port for the Proxy, either from JUPYTERHUB_SERVICE_URL or default values.
18+ See https://github.com/jupyterhub/jupyterhub/blob/4.x/jupyterhub/singleuser/mixins.py#L266-L284.
19+ """
20+ address = "127.0.0.1"
21+ port = 8888
22+
23+ if os .environ .get ("JUPYTERHUB_SERVICE_URL" ):
24+ url = urlparse (os .environ ["JUPYTERHUB_SERVICE_URL" ])
25+
26+ if url .hostname :
27+ address = url .hostname
28+
29+ if url .port :
30+ port = url .port
31+ elif url .scheme == "http" :
32+ port = 80
33+ elif url .scheme == "https" :
34+ port = 443
35+
36+ return address , port
1237
1338
1439def run (
1540 command : list [str ],
16- port : int ,
17- address : str ,
41+ port : int | None ,
42+ address : str | None ,
1843 server_port : int ,
1944 socket_path : str | None ,
2045 socket_auto : bool ,
@@ -34,8 +59,9 @@ def run(
3459 log .setLevel (logging .DEBUG )
3560 gen_log .setLevel (logging .DEBUG )
3661
37- if not port :
38- port = get_port_from_env ()
62+ address_port_default = _default_address_and_port ()
63+ address = address or address_port_default [0 ]
64+ port = port or address_port_default [1 ]
3965
4066 if skip_authentication :
4167 log .warn ("Disabling Authentication with JuypterHub Server!" )
@@ -84,18 +110,16 @@ def main():
84110 parser .add_argument (
85111 "-p" ,
86112 "--port" ,
87- default = 0 ,
88113 type = int ,
89114 dest = "port" ,
90- help = "Port for the proxy server to listen on (0 for JupyterHub default) ." ,
115+ help = "Set port for the proxy server to listen on. Will use 'JUPYTERHUB_SERVICE_URL' or '127.0.0.1' by default." ,
91116 )
92117 parser .add_argument (
93118 "-a" ,
94119 "--address" ,
95- default = "localhost" ,
96120 type = str ,
97121 dest = "address" ,
98- help = "Address for the proxy server to listen on." ,
122+ help = "Set address for the proxy server to listen on. Will use 'JUPYTERHUB_SERVICE_URL' or '8888' by default ." ,
99123 )
100124 parser .add_argument (
101125 "-s" ,
0 commit comments