How do you use an HTTP/HTTPS proxy with boto3?

孤人 提交于 2019-12-18 18:47:36

问题


On the old boto library is was simple enough to use the proxy, proxy_port, proxy_user and proxy_pass parameters when you open a connection. However, I could not find any equivalent way of programmatically define the proxy parameters on boto3. :(


回答1:


As of at least version 1.5.79, botocore accepts a proxies argument in the botocore config.

e.g.

import boto3
from botocore.config import Config

boto3.resource('s3', config=Config(proxies={'https': 'foo.bar:3128'}))

boto3 resource https://boto3.readthedocs.io/en/latest/reference/core/session.html#boto3.session.Session.resource

botocore config https://botocore.readthedocs.io/en/stable/reference/config.html#botocore.config.Config




回答2:


If you user proxy server hasn't password try as bellow

import os
os.environ["HTTP_PROXY"] = "http://proxy.com:port"
os.environ["HTTPS_PROXY"] = "https://proxy.com:port"

if you user proxy server has password try as bellow

import os
os.environ["HTTP_PROXY"] = "http://user:password@proxy.com:port"
os.environ["HTTPS_PROXY"] = "https://user:password@proxy.com:port"



回答3:


Apart from altering the environment variable, I'll present what I found in the code.

Since boto3 uses botocore, I had a look through the source code:

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

From this link, we end up at:

    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)

...which is called by proxies = self._get_proxies(final_endpoint_url) in the EndpointCreator class.

Long story short, if you're using python2 it will use the getproxies method from urllib2 and if you're using python3, it will use urllib3.

get_environ_proxies is expecting a dict containing {'http:' 'url'} (and I'm guessing https too).

You could always patch the code, but that is poor practice.




回答4:


This is one of the rare occasions when I would recommend monkey-patching, at least until the Boto developers allow connection-specific proxy settings:

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


来源:https://stackoverflow.com/questions/33480108/how-do-you-use-an-http-https-proxy-with-boto3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!