在旧库上,打开连接时使用 、 和 参数非常简单boto
。但是,我找不到任何等效的方式以编程方式在 boto3 上定义代理参数。:(proxy
proxy_port
proxy_user
proxy_pass
4 回答
至少从版本 1.5.79 开始,botocore 接受proxies
botocore 配置中的参数。
例如
import boto3
from botocore.config import Config
boto3.resource('s3', config=Config(proxies={'https': 'foo.bar:3128'}))
boto3 资源 https://boto3.readthedocs.io/en/latest/reference/core/session.html#boto3.session.Session.resource
botocore 配置 https://botocore.readthedocs.io/en/stable/reference/config.html#botocore.config.Config
如果您的用户代理服务器没有密码,请尝试以下操作:
import os
os.environ["HTTP_PROXY"] = "http://proxy.com:port"
os.environ["HTTPS_PROXY"] = "https://proxy.com:port"
如果您的用户代理服务器有密码,请尝试以下操作:
import os
os.environ["HTTP_PROXY"] = "http://user:password@proxy.com:port"
os.environ["HTTPS_PROXY"] = "https://user:password@proxy.com:port"
除了更改环境变量之外,我还将展示我在代码中找到的内容。
由于boto3使用了botocore,所以我看了一下源代码:
从这个链接,我们最终到达:
def _get_proxies(self, url):
# We could also support getting proxies from a config file,
# but for now proxy support is taken from the environment.
return get_environ_proxies(url)
proxies = self._get_proxies(final_endpoint_url)
...在EndpointCreator
课堂上被调用。
长话短说,如果您使用的是 python2,它将使用getproxies
urllib2 中的方法,如果您使用的是 python3,它将使用 urllib3。
get_environ_proxies
期待一个包含{'http:' 'url'}
(我https
也在猜测)的字典。
您总是可以patch
编写代码,但这是不好的做法。
这是我建议使用猴子补丁的少数情况之一,至少在 Boto 开发人员允许连接特定的代理设置之前:
import botocore.endpoint
def _get_proxies(self, url):
return {'http': 'http://someproxy:1234/', 'https': 'https://someproxy:1234/'}
botocore.endpoint.EndpointCreator._get_proxies = _get_proxies
import boto3