我正试图在工作中通过代理使用pip。

这篇文章中的一个答案建议使用CNTLM。我安装和配置它根据这另一个帖子,但运行cntlm.exe -c cntlm.ini -I -M http://google.com给出错误连接到代理失败,退出。

我还尝试了pip install——proxy=user:pass@localhost:3128(默认的CNTLM端口),但会引发无法获取索引基础URL http://pypi.python.org/simple/。很明显代理出问题了。

有人知道如何更明确地检查CNTLM是否设置正确,或者是否有另一种方法完全绕过这个问题吗?我知道你也可以像这里描述的那样设置http_proxy环境变量,但我不确定要放入什么凭证。来自cntlm.ini的那些?


当前回答

打开Windows命令提示符。

设置代理环境变量。

set http_proxy=http://user:password@proxy_ip:port
set https_proxy=https://user:password@proxy_ip:port

在相同的Windows命令提示符中使用代理安装Python包。

pip install --proxy="user:password@proxy_ip:port" package_name

其他回答

对于windows;在命令提示符中将代理设置为 设置HTTP_PROXY =域\用户名:password@myproxy: myproxyport

例如:set http_proxy=IND\namit.kewat:xl123456@192.168.180.150:8880

在Windows下不要忘记设置

SET HTTPS_PROXY=<proxyHost>:<proxyPort>

我需要设置什么

pip install pep8

在带有身份验证的工作代理后使用pip,请注意,在使用用户和密码指定代理url时,某些操作系统需要引号:

pip install <module> --proxy 'http://<proxy_user>:<proxy_password>@<proxy_ip>:<proxy_port>'

文档:https://pip.pypa.io/en/stable/user_guide/使用代理服务器

例子:

pip3 install -r requirements.txt --proxy 'http://user:password@192.168.0.1:1234'

例子:

pip install flask --proxy 'http://user:password@192.168.0.1:1234'

代理也可以在pip.ini中手动配置。 例子:

[global]
proxy = http://user:password@192.168.0.1:1234 

文档:https://pip.pypa.io/en/stable/user_guide/配置文件

我得到了错误:

chris@green:~$ sudo http_proxy=http://localhost:3128 pip install django==1.8.8 
Downloading/unpacking django==1.8.8
  Cannot fetch index base URL http://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement django==1.8.8
No distributions at all found for django==1.8.8
Storing complete log in /home/chris/.pip/pip.log

(代理服务器的端口是ssh端口转发到localhost:3128)。

我必须同时设置http和https代理使其工作:

chris@green:~$ sudo http_proxy=http://localhost:3128 https_proxy=http://localhost:3128 pip install django==1.8.8
Downloading/unpacking django==1.8.8
  Downloading Django-1.8.8.tar.gz (7.3Mb): 7.3Mb downloaded
  Running setup.py egg_info for package django

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
  Running setup.py install for django

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
    changing mode of build/scripts-2.7/django-admin.py from 644 to 755
    changing mode of /usr/local/bin/django-admin.py to 755
    Installing django-admin script to /usr/local/bin
Successfully installed django
Cleaning up...

http://pypi.python.org/simple/重定向到https://pypi.python.org/simple,但pip的错误没有告诉你。

要为windows设置CNTLM,请遵循本文。对于Ubuntu,请阅读我的博客文章。

编辑:

基本上,要在任何平台上使用CNTLM,在使用http://127.0.0.1:3128作为父代理的代理之前,需要设置您的用户名和散列密码。

编辑配置并添加重要信息,如域,用户名,密码和父代理。 生成哈希密码。 Windows cntlm -c cntlm.ini -H Ubuntu/Linux cntlm -v -H -c /etc/cntlm.conf 从配置中删除纯文本密码,并用生成的密码替换它们。

检查是否工作:

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

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

有关更详细的说明,请参阅上面的链接。

更新:

为了完整起见,我最近能够在Windows中配置和使用CNTLM。我在PC版Kindle的同步过程中遇到了一个问题,因为我们的代理,安装和配置CNTLM for Windows为我解决了这个问题。更多细节请参考我的文章。