Python 在代理后面使用 Jupyter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36629707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:08:44  来源:igfitidea点击:

Using Jupyter behind a proxy

pythonanacondajupyterconda

提问by tog

Is there a similar config to that of .condarc (anaconda 4.0.0) that allows Jupyter to be configured to work behind a corporate proxy on a local machine?

是否有与 .condarc(anaconda 4.0.0)类似的配置,允许将 Jupyter 配置为在本地机器上的公司代理后面工作?

Error received:

收到错误:

HTTPError: HTTP Error 407: Proxy Authentication Required

回答by Boern

Way easier: Just add the following to your notebook:

更简单的方法:只需将以下内容添加到您的笔记本中:

In [1]: import os
        os.environ['http_proxy'] = "http://user:passwd@host:port" 
        os.environ['https_proxy'] = "https://user:passwd@host:port" 

after that, requests will work OK=200, e.g.

之后,请求将正常工作OK=200,例如

In [2]: import requests
        requests.get("http://google.com")
Out[2]: <Response [200]>

回答by Jamie Bull

Based on this link.

基于此链接

You have to modify the Jupyter notebook server env. Create a file named 00-something.pyunder your Jupyter notebook server profile and add the following:

您必须修改 Jupyter 笔记本服务器环境。创建一个以00-something.py您的 Jupyter 笔记本服务器配置文件命名的文件并添加以下内容:

For example:

例如:

vi /.jupyter/profile_myserver/startup/00-startup.py

(or on Windows open up "C:/Users/your username/.jupyter/profile_myserver/startup/00-startup.py" in your editor of choice)

(或在 Windows 上,在您选择的编辑器中打开“C:/Users/您的用户名/.jupyter/profile_myserver/startup/00-startup.py”)

and add

并添加

import sys,os,os.path
os.environ['HTTP_PROXY']="http://proxy.example.com:80"
os.environ['HTTPS_PROXY']="https://proxy.example.com:443"

you can confirm the env variables by running

您可以通过运行来确认环境变量

%env

in a cell and the output

在一个单元格和输出

{'CLICOLOR': '1',
'GIT_PAGER': 'cat',
'HOME': '/home/jay',
'HTTP_PROXY': 'http://proxy.example.com:80',
..

Next try

下次试试

import requests
requests.get("http://google.com")

If you get a response [200] then you are all set.

如果您收到响应 [200],那么您已准备就绪。

回答by Gunawan Marbun

Use the lowercase variable instead, it works for me:

改用小写变量,它对我有用:

import sys,os,os.path
os.environ['http_proxy']="http://user:passwd@host:port"
os.environ['https_proxy']="http://user:passwd@host:port"

Then check your env variable using this:

然后使用以下命令检查您的 env 变量:

%env

The output will be like this:

输出将是这样的:

{'CLICOLOR': '1',
 '...'
 '...'
 'http_proxy': 'http://gunawan.marbun:[email protected]:8080'
 'https_proxy': 'https://gunawan.marbun:[email protected]:8080'
 'no_proxy': 'localhost,127.0.0.0/8,::1'}

Notes: Since I can't comment due to my reputation (req 50 and I'm newbie), I present new answer instead.

注意:由于我的声誉(要求 50 并且我是新手)而无法发表评论,因此我提出了新的答案。

回答by Megatron

Based on these Jupyter customization instructions:

基于这些Jupyter 自定义说明

  1. Create the directory .jupyter_configin your home directory
  2. Add the line JUPYTER_CONFIG_DIR=~/.jupyter_configto your bash/shell profile (eg. .bash_profile).
  3. Add a script titled startup.pyto ~/.jupyter_configwith the following code, customized with your specific proxy information:
  1. .jupyter_config在您的主目录中创建目录
  2. 将该行添加JUPYTER_CONFIG_DIR=~/.jupyter_config到您的 bash/shell 配置文件(例如.bash_profile)。
  3. 使用以下代码添加标题为startup.pyto的脚本,~/.jupyter_config并使用您的特定代理信息进行自定义:
import os
os.environ['http_proxy']= "http://user:passwd@host:port"
os.environ['https_proxy']= "https://user:passwd@host:port"
os.environ['HTTP_PROXY']= os.environ['http_proxy']
os.environ['HTTPS_PROXY']= os.environ['https_proxy']

回答by Pedro H. N. Vieira

An easier solution for me was to add an exception to my proxy configuration. I just put the address http://localhost:8888to my exception list and it worked.

对我来说,一个更简单的解决方案是在我的代理配置中添加一个例外。我只是将地址添加http://localhost:8888到我的例外列表中并且它起作用了。