Python 使用经过身份验证的代理请求而不公开用户名和密码。
Python request with authenticated proxy without exposing username and password.
我正在使用请求库连接到 api 并获取数据。我有要连接的代理,它适用于 AuthenticatedProxy
这就是我的代码的样子。
import requests
s = requests.Session()
url = "https://testapi"
urlkey = “testkey”
proxies = {'https': 'http://<UserName>:<Password>@proxy_url:8080'}
resp = s.get(url, params={'key':urlkey }, proxies = proxies)
content = resp.content
print content
用户名和密码在这里暴露,我想避免这种情况。我怎样才能做到这一点?我可以请求使用 defaultCredentials 或 'account that is running the python script' 凭据吗?
在 .net 中,以下配置有效:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy proxyaddress="https://testapi:8080" bypassonlocal="True"/>
</defaultProxy>
</system.net>
谢谢大家。
类似问题here.
来自request
docs:
You can also configure proxies by setting the environment variables
HTTP_PROXY and HTTPS_PROXY.
因此您可以通过设置环境变量来避免在脚本中泄露凭据:
$ export HTTPS_PROXY="http://<UserName>:<Password>@proxy_url:8080"
然后在脚本中,您只需调用即可使用代理:
resp = s.get(url, params={'key':urlkey })
我正在使用请求库连接到 api 并获取数据。我有要连接的代理,它适用于 AuthenticatedProxy
这就是我的代码的样子。
import requests
s = requests.Session()
url = "https://testapi"
urlkey = “testkey”
proxies = {'https': 'http://<UserName>:<Password>@proxy_url:8080'}
resp = s.get(url, params={'key':urlkey }, proxies = proxies)
content = resp.content
print content
用户名和密码在这里暴露,我想避免这种情况。我怎样才能做到这一点?我可以请求使用 defaultCredentials 或 'account that is running the python script' 凭据吗?
在 .net 中,以下配置有效:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy proxyaddress="https://testapi:8080" bypassonlocal="True"/>
</defaultProxy>
</system.net>
谢谢大家。 类似问题here.
来自request
docs:
You can also configure proxies by setting the environment variables HTTP_PROXY and HTTPS_PROXY.
因此您可以通过设置环境变量来避免在脚本中泄露凭据:
$ export HTTPS_PROXY="http://<UserName>:<Password>@proxy_url:8080"
然后在脚本中,您只需调用即可使用代理:
resp = s.get(url, params={'key':urlkey })