26

在旧库上,打开连接时使用 、 和 参数非常简单boto。但是,我找不到任何等效的方式以编程方式在 boto3 上定义代理参数。:(proxyproxy_portproxy_userproxy_pass

4

4 回答 4

31

至少从版本 1.5.79 开始,botocore 接受proxiesbotocore 配置中的参数。

例如

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

于 2017-08-03T18:39:28.820 回答
17

如果您的用户代理服务器没有密码,请尝试以下操作:

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"
于 2015-11-03T14:09:37.330 回答
3

除了更改环境变量之外,我还将展示我在代码中找到的内容。

由于boto3使用了botocore,所以我看了一下源代码:

https://github.com/boto/botocore/blob/66008c874ebfa9ee7530d944d274480347ac3432/botocore/endpoint.py#L265

从这个链接,我们最终到达:

    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,它将使用getproxiesurllib2 中的方法,如果您使用的是 python3,它将使用 urllib3。

get_environ_proxies期待一个包含{'http:' 'url'}(我https也在猜测)的字典。

您总是可以patch编写代码,但这是不好的做法。

于 2015-11-03T14:34:35.950 回答
1

这是我建议使用猴子补丁的少数情况之一,至少在 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
于 2016-03-24T20:21:19.143 回答