我有一个正在工作的Jupyterhub和笔记本电脑,已经按照https://zero-to-jupyterhub.readthedocs.io/en/stable/上的文档操作了-很好,我可以生成笔记本电脑,使用它们,等等。
我以Hubshare(https://github.com/jupyterhub/hubshare)为例,我正在运行一些东西:Hub日志报告
Managed service nbexchange running at http://127.0.0.1:9090
因此服务代码有效。
我设置了以下三个句柄:
from .base import BaseHandler
from tornado import web
class WhoAmI(BaseHandler):
urls = ['/whoami']
@web.authenticated
def get(self):
self.log.info(self.get_current_user())
class EmptyHandler(BaseHandler):
urls = ['/empty']
def get(self):
self.log.info("Hello World")
class HomeHandler(BaseHandler):
urls = ['/']
def get(self):
self.log.info("Hello World, this is home")
default_handlers = [
WhoAmI,
EmptyHandler,
HomeHandler
]
如果我执行hub
Docker容器,我可以做
curl http://127.0.0.1:9090/services/nbexchange/empty
并正确显示中心日志
NbExchange apihandlers:13] Hello World
当我尝试卷曲whoami
路由时,没有得到任何回报-如预期的那样:它需要一个web_authenticated连接,并且curl
不提供任何身份验证。
当我在jupyterhub服务中启动笔记本时,我在单元格中编写了以下代码:
import requests
# URL is same as curl command
url = 'http://127.0.0.1:9090'
address = url + '/services/nbexchange/empty'
print(address)
r = requests.get( address )
我运行单元格,它会打印地址
http://127.0.0.1:9090/services/nbexchange/empty
...然后失败。
这并不令我感到惊讶,因为笔记本的“ Web服务器”与集线器的“ Web服务器”不同,并且 localhost 应该会失败。
我将代码更改为要读取
import requests
# Get URL from notebook environment variables
url = 'http://{}:{}'.format(os.environ['HUB_SERVICE_HOST'],os.environ['HUB_SERVICE_PORT'])
address = url + '/services/nbexchange/empty'
print(address)
r = requests.get( address )
并重新运行单元格,现在将打印地址
http://10.43.190.164:8081/services/nbexchange/empty
但是中心日志显示重定向:
302 GET /services/nbexchange/empty → /hub/services/nbexchange/empty (@10.42.2.234) 0.77ms
404 GET /hub/services/nbexchange/empty (@10.42.2.234) 1.43ms
嗯.....
非常感谢。