Numpy 配置notebook服务器

需要考虑位于公共域中的notebook服务器的安全性。为此,需要设置密码和使用SSL证书来建立连接。我们需要用证书实现基于https的安全通信。更多相关信息请见 https://en.wikipedia.org/wiki/Transport_Layer_Security。

具体步骤

下述步骤介绍了怎样配置安全的notebook服务器。

  1. 生成密码。可以在IPython环境中生成一个密码。启动一个新的IPython会话,键入如下命令:
In [1]: from IPython.lib import passwd

In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:0e422dfccef2:84cfbcb 
b3ef95872fb8e23be3999c123f862d856' 

输入第二行命令后,系统会提示你输入密码。你需要记住这个密码。之后会输出一个长字符串。复制该字符串,后面我们要用到它。

  1. 创建SSL证书。为了能创建SSL证书,你需要让openssl命令位于可访问路径中。

openssl命令行工具的设置过程不算太复杂,但可能有一些棘手的细节。遗憾的是,这些内容超出了本书的范围,好在网上有很多教程可以为你提供帮助。

执行如下命令,生成一个文件名为mycert.pem的证书:

$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
Generating a 1024 bit RSA private key
......++++++
........................++++++
writing new private key to 'mycert.pem'
-----You are about to be asked to enter information that will be incorporated into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:
Email Address []:

openssl会提示你在一些区域里填写内容,请参阅相关的手册页了解更多信息。

  1. 创建一个服务器配置。使用如下命令为服务器创建一个专用的配置:
ipython profile create nbserver
  1. 编辑配置文件。对配置文件进行编辑。在本例中,配置文件所在的位置是 ~/.ipython/profile_nbserver/ipython_notebook_config.py。

这个配置文件包括很多行内容,这里就不列出了。我们至少需要修改如下几行内容:

c.NotebookApp.certfile = u'/absolute/path/to/your/certificate'
c.NotebookApp.password = u'sha1:b...your password'
c.NotebookApp.port = 9999  

注意我们做的改动是,把证书位置指向了我们刚才创建的SSL证书,设置了密码,并且把端口号改为9999。

  1. 启动服务器。使用如下命令启动服务器,检查所做的修改是否已生效。
ipython notebook --profile=nbserver
[NotebookApp] Using existing profile dir: u'/Users/ivanidris/.ipython/profile_nbserver'

[NotebookApp] The IPython Notebook is running at:https://127.0.0.1:9999

[NotebookApp] Use Control-C to stop this server and shut down all kernels.

服务器已运行在9999端口,你需要使用https方式连接到该服务器。如果一切顺利,我们会看到一个登录页面。你很可能还需要接受浏览器发来的一个安全例外警告。

Numpy 配置notebook服务器

攻略小结

我们为公共域服务器创建了一个专用的配置。IPython中已有一些配置样本存在,例如默认配置。创建配置时,会在.ipython目录下添加一个profile_<profilename>文件夹,文件夹中包括配置文件等内容。通过使用--profile=<profile_name>命令行选项,可以加载指定的配置。用如下命令可以列出已经存在的配置。

ipython profile list

Available profiles in IPython:
    cluster
    math
    pysh
    python3

    The first request for a bundled profile will copy it
    into your IPython directory (/Users/ivanidris/.ipython),
    where you can customize it.

Available profiles in /Users/ivanidris/.ipython:
    default
    nbserver
    sh 

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程