requests proxy使用

最近在通过代理连接ES数据库时,一直报错

requests.packages.urllib3.exceptions.ProxySchemeUnknown: Not supported proxy scheme socks5

from elasticsearch import Elasticsearch, RequestsHttpConnection
class MyConnection(RequestsHttpConnection):
    def __init__(self, *args, **kwargs):
        proxies = kwargs.pop('proxies', {})
        super(MyConnection, self).__init__(*args, **kwargs)
        self.session.proxies = proxies

esclient = Elasticsearch(["ip:port"],
                                     connection_class=MyConnection,
                                     proxies={'http': 'socks5://ip:port', 'https': "socks5://ip:port"},
                                     timeout=300,
                                     max_retries=10,
                                     retry_on_timeout=True)

 简直是黔驴技穷了,最后发现做如下操作,就可以让requests支持socks5操作,特此与需要的人分享

# python3 -m pip install -U requests[socks]

此操作在使用requests库爬虫时用proxies道理一样

你可能感兴趣的:(ElasticSearch,python)