Skip to content

Commit c0d457d

Browse files
committed
Extract address from JUPYTERHUB_SERVICE_URL
1 parent e561072 commit c0d457d

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

jupyter_server_proxy/standalone/__init__.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
11
import argparse
22
import logging
33
import os
4+
from urllib.parse import urlparse
45

56
from tornado import ioloop
67
from tornado.httpserver import HTTPServer
78
from tornado.log import app_log as log
89
from tornado.log import enable_pretty_logging, gen_log
910

1011
from .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

1439
def 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",

jupyter_server_proxy/standalone/proxy.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import re
33
from logging import Logger
4-
from urllib.parse import urlparse
54

65
from jupyterhub import __version__ as __jh_version__
76
from jupyterhub.services.auth import HubOAuthCallbackHandler, HubOAuthenticated
@@ -148,16 +147,3 @@ def __init__(self, *args, **kwargs):
148147
)
149148

150149
return app
151-
152-
153-
# https://github.com/jupyterhub/jupyterhub/blob/2.0.0rc3/jupyterhub/singleuser/mixins.py#L340-L349
154-
def get_port_from_env():
155-
if os.environ.get("JUPYTERHUB_SERVICE_URL"):
156-
url = urlparse(os.environ["JUPYTERHUB_SERVICE_URL"])
157-
if url.port:
158-
return url.port
159-
elif url.scheme == "http":
160-
return 80
161-
elif url.scheme == "https":
162-
return 443
163-
return 8888

0 commit comments

Comments
 (0)