Python 请求 API 使用代理进行 https 请求获取 407 需要代理身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34025964/
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 14:21:38  来源:igfitidea点击:

Python requests API using proxy for https request get 407 Proxy Authentication Required

pythonproxypython-requests

提问by Fabrice E.

I've been struggling with my company proxy to make an https request.

我一直在努力使用我的公司代理来发出 https 请求。

import requests
from requests.auth import HTTPProxyAuth

proxy_string = 'http://user:password@url_proxt:port_proxy'

s = requests.Session()
s.proxies = {"http": proxy_string , "https": proxy_string}
s.auth = HTTPProxyAuth(user,password)

r = s.get('http://www.google.com') # OK
print(r.text)
r = s.get('https://www.google.com',proxies={"http": proxy_string , "https": proxy_string}) #OK
print(r.text)
r = s.get('https://www.google.com') # KO
print(r.text)

When KO, I have the following exception :

当 KO 时,我有以下异常:

HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))

I looked online but didn't find someone having this specific issue with HTTPS.

我在网上查看,但没有发现有人对 HTTPS 有此特定问题。

Thank you for your time

感谢您的时间

回答by Fabrice E.

Thanks to the amazing help of Lukasa, I solved my issue.

感谢 Lukasa 的惊人帮助,我解决了我的问题。

Please see discussion on fix here : https://github.com/kennethreitz/requests/issues/2911

请在此处查看有关修复的讨论:https: //github.com/kennethreitz/requests/issues/2911

or set :

或设置:

session.trust_env=False

回答by Somit

I personally solved the above problem on my system by updating the environment variables http_proxy,https_proxy,socks_proxy,ftp_proxy.

我个人通过更新环境变量 http_proxy,https_proxy,socks_proxy,ftp_proxy 在我的系统上解决了上述问题。

First enter the command on your terminal : printenv

首先在终端上输入命令:printenv

This should show you the environment variables on your system.

这应该会向您显示系统上的环境变量。

In my case intially: http_proxy=http://proxyserver:port/

就我而言,最初是:http_proxy= http://proxyserver:port/

I changed it to : http_proxy=http://username:password@proxy:port/

我将其更改为:http_proxy= http://username:password@proxy:port/

using the command export http_proxy="http://username:password@proxy:port/"

使用命令 export http_proxy="http://username:password@proxy:port/"

Similarly for https_proxy,socks_proxy,ftp_proxy

同样对于 https_proxy,socks_proxy,ftp_proxy

回答by Vinay

Other way i have resolved is - speak with your corporate IT administrator and find a direct proxy port which connects to external domain (with / without password)

我解决的其他方法是 - 与您的公司 IT 管理员交谈并找到一个连接到外部域的直接代理端口(带/不带密码)

pip install --proxy=http://proxyhost:proxy_portpixiedust

pip install --proxy= http://proxyhost: proxy_portpixiedust

Found from other colleagues using the proxy (proxy_port direct connection) in their eclipse settings (network)

从其他同事在他们的 eclipse 设置(网络)中使用代理(proxy_port 直接连接)找到的