Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pip behind a proxy with CNTLM

Tags:

python

pip

proxy

People also ask

How do I get pip to work behind a proxy server?

pip can be configured to connect through a proxy server in various ways: using the --proxy command-line option to specify a proxy in the form scheme://[user:passwd@]proxy.server:port. using proxy in a Configuration Files. by setting the standard environment-variables http_proxy , https_proxy and no_proxy .

What is a pip proxy?

A proxy server for pip is most commonly used for security and privacy, but can also be used for control: Security & Privacy – a proxy server can be used in combination with firewalls in order to improve internal network security since requests from users on the local network are anonymized via a proxy server.

How do you pass a proxy in Python?

To use a proxy in Python, first import the requests package. Next create a proxies dictionary that defines the HTTP and HTTPS connections. This variable should be a dictionary that maps a protocol to the proxy URL. Additionally, make a url variable set to the webpage you're scraping from.


With Ubuntu I could not get the proxy option to work as advertised – so following command did not work:

sudo pip --proxy http://web-proxy.mydomain.com install somepackage

But exporting the https_proxy environment variable (note its https_proxy not http_proxy) did the trick:

export https_proxy=http://web-proxy.mydomain.com

then

sudo -E pip install somepackage

Under Windows dont forget to set

SET HTTPS_PROXY=<proxyHost>:<proxyPort>

what I needed to set for

pip install pep8

To setup CNTLM for windows, follow this article. For Ubuntu, read my blog post.

Edit:

Basically, to use CNTLM in any platform, you need to setup your username and hashed password, before using http://127.0.0.1:3128 as a proxy to your parent proxy.

  1. Edit the config and add important information like domain, username, password and parent proxy.

  2. Generate hashed password.

    Windows cntlm –c cntlm.ini –H

    Ubuntu/Linux cntlm -v -H -c /etc/cntlm.conf

  3. Remove plain text password from the config and replace them with the generated passwords.

To check if working:

Windows cntlm –M http://www.google.com

Ubuntu/Linux sudo cntlm -M http://www.google.com/

For more detailed instructions, see links above.

Update:

Just for completeness sake, I was able to configure and use CNTLM in Windows recently. I encountered a problem during the syncing process of Kindle for PC because of our proxy and installing and configuring CNTLM for Windows fixed that issue for me. Refer to my article for more details.


It was not working for me. I had to use https at work:

pip install --proxy=https://user@mydomain:port somepackage

In order to update, add -U.


You can continue to use pip over HTTPS by adding your corporation's root certificate to the cacert.pem file in your site-packages/pip folder. Then configure pip to use your proxy by adding the following lines to ~/pip/pip.conf (or ~\pip\pip.ini if you're on Windows):

[global]
proxy = [user:passwd@]proxy.server:port

That's it. No need to use third party packages or give up HTTPS (of course, your network admin can still see what you're doing).


for windows; set your proxy in command prompt as
set HTTP_PROXY=domain\username:password@myproxy:myproxyport

example:
set http_proxy=IND\namit.kewat:[email protected]:8880


This worked for me (on Windows via CMD):

pip install --proxy proxyserver:port requests