Jupyter Server可从外部访问,但拒绝localhost连接 [英] Jupyter Server Reachable from outside but refuse localhost connection
问题描述
我已经使用以下配置在计算机A上安装了Jupyter Notebook服务器:
I've Setup a Jupyter Notebook Server on computer A with the following configuration:
c.NotebookApp.certfile = '.jupyter/mycert.pem'
c.NotebookApp.ip = 'MY_IP_ADDRESS'
c.NotebookApp.keyfile = '.jupyter/mykey.key'
c.NotebookApp.notebook_dir = 'Dropbox/GitHub'
c.NotebookApp.password = 'MY_HASHED_PASSWORD'
c.NotebookApp.port = 8888
服务器启动时,计算机B可以通过 https://MY_IP_ADDRESS:8888
访问此服务器.
When the server starts, computer B can reach to this server with https://MY_IP_ADDRESS:8888
.
但是计算机A无法以任何形式到达自身
But Computer A couldn't reach to itself in any kind
# ERR_CONNECTION_REFUSED
https://localhost:8888/
https://127.0.0.1:8888/
# ERR_CONNECTION_TIMED_OUT
https://MY_IP_ADDRESS:8888/
值得一提的是,当我注释掉分配IP地址的行时, localhost
和 127.0.0.1
可以正常工作,但是我无法连接从外部(计算机B)
Worth mentioned is that when I comment out the line which assign the IP address, localhost
and 127.0.0.1
works fine, but I wouldn't be able to connect from outside (computer B)
我已经尝试过切换端口,切换浏览器,配置/etc/hosts
和/etc/resolv.conf
以及〜/内的其他一些选项.jupyter/jupyter_notebook_config.py
I've tried switching port, switching browsers, configure /etc/hosts
and /etc/resolv.conf
and some other options inside ~/.jupyter/jupyter_notebook_config.py
但它们似乎都不起作用
任何建议将不胜感激.
but none of them seems to work
Any suggestion would be appreciated.
经过进一步调查,结果证明这与jupyter笔记本电脑无关.我运行了一个带有以下代码段的简单python HTTP服务器:
After some further investigation, it turns out this has nothing to do with jupyter notebook. I ran a simple python HTTP server with the following snippet:
import sys
from http.server import SimpleHTTPRequestHandler
import http.server
def test(HandlerClass=SimpleHTTPRequestHandler,
ServerClass=http.server.HTTPServer):
protocol = "HTTP/1.0"
host = 'MY_IP_ADDRESS'
port = 8888
if len(sys.argv) > 1:
arg = sys.argv[1]
if ':' in arg:
host, port = arg.split(':')
port = int(port)
else:
try:
port = int(sys.argv[1])
except:
host = sys.argv[1]
server_address = (host, port)
HandlerClass.protocol_version = protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
if __name__ == "__main__":
test()
结果是相同的.我猜这实际上是正常现象,但不是我的偏爱:(
The results are the same. I'm guessing this is actually a normal behavior, but just not my favor:(
因此导致两种情况:
-
用于自连接
for self-connecting
- 绑定IP地址:"(空字符串)
- 使用localhost或127.0.0.1
- (但其他客户端将无法连接)
用于其他客户端连接
- binging IP地址:"MY_IP_ADDRESS"
- 使用MY_IP_ADDRESS:8888
- (但是服务器本身无法连接)
我添加了一些新标签,希望任何人都可以对此进行提示><
I've added some new tag, hope anyone will have hint on this><
推荐答案
我将回答我自己的古老问题.
I'll be answering my own ancient question.
为了在每个地址上绑定服务,请使用 0.0.0.0 特殊地址.
In order to bind service on every address, use the 0.0.0.0 special address.
这篇关于Jupyter Server可从外部访问,但拒绝localhost连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!