Using Jupyter behind a proxy

2020-05-19 05:06发布

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?

Error received:

HTTPError: HTTP Error 407: Proxy Authentication Required

5条回答
够拽才男人
2楼-- · 2020-05-19 05:48

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

The output will be like this:

{'CLICOLOR': '1',
 '...'
 '...'
 'http_proxy': 'http://gunawan.marbun:xxxxxxxx@cache.itb.ac.id:8080'
 'https_proxy': 'https://gunawan.marbun:xxxxxxxx@cache.itb.ac.id: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.

查看更多
Lonely孤独者°
3楼-- · 2020-05-19 05:58

Based on this link.

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

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)

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.

查看更多
我命由我不由天
4楼-- · 2020-05-19 05:58

Based on these Jupyter customization instructions:

  1. Create the directory .jupyter_config in your home directory
  2. Add the line JUPYTER_CONFIG_DIR=~/.jupyter_config to your bash/shell profile (eg. .bash_profile).
  3. Add a script titled startup.py to ~/.jupyter_config with the following code, customized with your specific proxy information:
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']
查看更多
混吃等死
5楼-- · 2020-05-19 06:04

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.

In [2]: import requests
        requests.get("http://google.com")
Out[2]: <Response [200]>
查看更多
家丑人穷心不美
6楼-- · 2020-05-19 06:12

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

查看更多
登录 后发表回答