Using Jupyter behind a proxy

37,715

Solution 1

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("https://google.com")
Out[2]: <Response [200]>

Solution 2

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.

Solution 3

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:[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.

Solution 4

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.

Solution 5

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']
Share:
37,715
tog
Author by

tog

Updated on September 13, 2020

Comments

  • tog
    tog over 3 years

    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