本文整理汇总了Python中urllib3.ProxyManager方法的典型用法代码示例。如果您正苦于以下问题:Python urllib3.ProxyManager方法的具体用法?Python urllib3.ProxyManager怎么用?Python urllib3.ProxyManager使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib3
的用法示例。
在下文中一共展示了urllib3.ProxyManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def __init__(self, url, user_agent, cookies_string=False, custom_header=False, insecure_ssl='false', proxy=False):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
self.__url = url
self.__headers = dict()
self.__headers['User-Agent'] = self.__default_user_agent if user_agent == 'default' else user_agent
if cookies_string:
self.__headers['Cookie'] = cookies_string
if custom_header:
self.__parse_custom_header(custom_header)
self.__verify = 'CERT_REQUIRED' if insecure_ssl == 'false' else 'CERT_NONE'
if proxy:
proxy_type = proxy.split('://')[0]
if proxy_type == 'http' or proxy_type == 'https':
self.__request_obj = urllib3.ProxyManager(proxy, ssl_version=ssl.PROTOCOL_TLSv1,
timeout=self.__request_timeout, cert_reqs=self.__verify)
else:
self.__request_obj = SOCKSProxyManager(proxy, ssl_version=ssl.PROTOCOL_TLSv1,
timeout=self.__request_timeout, cert_reqs=self.__verify)
else:
self.__request_obj = urllib3.PoolManager(ssl_version=ssl.PROTOCOL_TLSv1, timeout=self.__request_timeout,
cert_reqs=self.__verify)
# print (vars(self))
示例2: init
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def init():
logger = logging.getLogger(__name__)
global __HTTP
proxy_url = os.getenv("http_proxy")
if proxy_url and len(proxy_url) > 0:
parsed_url = urllib3.util.parse_url(proxy_url)
logger.info("Connecting via proxy URL [%s] to the Internet (picked up from the env variable [http_proxy]).",
proxy_url)
__HTTP = urllib3.ProxyManager(proxy_url,
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where(),
# appropriate headers will only be set if there is auth info
proxy_headers=urllib3.make_headers(proxy_basic_auth=parsed_url.auth))
else:
logger.info("Connecting directly to the Internet (no proxy support).")
__HTTP = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
示例3: __init__
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def __init__(self, url, *args, **kwargs):
super(Transport, self).__init__(url, *args, **kwargs)
url_parts = compat.urlparse.urlparse(url)
pool_kwargs = {"cert_reqs": "CERT_REQUIRED", "ca_certs": certifi.where(), "block": True}
if self._server_cert and url_parts.scheme != "http":
pool_kwargs.update(
{"assert_fingerprint": self.cert_fingerprint, "assert_hostname": False, "cert_reqs": ssl.CERT_NONE}
)
del pool_kwargs["ca_certs"]
elif not self._verify_server_cert and url_parts.scheme != "http":
pool_kwargs["cert_reqs"] = ssl.CERT_NONE
pool_kwargs["assert_hostname"] = False
proxies = compat.getproxies_environment()
proxy_url = proxies.get("https", proxies.get("http", None))
if proxy_url and not compat.proxy_bypass_environment(url_parts.netloc):
self.http = urllib3.ProxyManager(proxy_url, **pool_kwargs)
else:
self.http = urllib3.PoolManager(**pool_kwargs)
示例4: set_proxy
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def set_proxy(url, basic_auth=None):
"""
Access Bot API through a proxy.
:param url: proxy URL
:param basic_auth: 2-tuple ``('username', 'password')``
"""
global _pools, _onetime_pool_spec
if not url:
_pools['default'] = urllib3.PoolManager(**_default_pool_params)
_onetime_pool_spec = (urllib3.PoolManager, _onetime_pool_params)
elif basic_auth:
h = urllib3.make_headers(proxy_basic_auth=':'.join(basic_auth))
_pools['default'] = urllib3.ProxyManager(url, proxy_headers=h, **_default_pool_params)
_onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=url, proxy_headers=h, **_onetime_pool_params))
else:
_pools['default'] = urllib3.ProxyManager(url, **_default_pool_params)
_onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=url, **_onetime_pool_params))
示例5: setProxyPool
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def setProxyPool(self, proxy):
scheme = proxy.split(':')[0]
if scheme in ('http', 'https'):
ProxyManager = urllib3.ProxyManager
elif scheme in ('socks4', 'socks5'):
ProxyManager = SOCKSProxyManager
else:
print("Wrong Proxy Format: " + proxy)
print("Proxy should start with http/https/socks4/socks5 .")
input()
raise SystemExit
# maxsize is the max. number of connections to the same server
return [ProxyManager(proxy, num_pools=10, maxsize=8, timeout=self.timeout, **self.sslparams),
ProxyManager(proxy, num_pools=10, maxsize=8, timeout=self.timeout)]
示例6: __init__
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def __init__(self, handler, range_start, range_end):
self.handler = handler
self.write = handler.wfile.write
self.url = handler.url
self.scheme = handler.url_parts.scheme
self.netloc = handler.url_parts.netloc
self.headers = dict((k.title(), v) for k, v in handler.headers.items())
self.headers['Host'] = self.netloc
self.headers.update(fake_headers)
self.range_start = range_start
self.range_end = range_end
self.delay_cache_size = self.max_size * self.threads * 4
self.delay_star_size = self.delay_cache_size * 2
self.max_threads = min(self.threads * 2, self.pool_size)
if self.http is None:
connection_pool_kw = {
'block': True,
'timeout': self.timeout,
'maxsize': self.pool_size
}
if self.proxy:
if self.proxy.lower().startswith('socks'):
from urllib3.contrib.socks import SOCKSProxyManager as ProxyManager
else:
ProxyManager = urllib3.ProxyManager
http = ProxyManager(self.proxy, **connection_pool_kw)
if ProxyManager is not urllib3.ProxyManager:
http.connection_pool_kw['_socks_options']['rdns'] = True
else:
http = urllib3.PoolManager(**connection_pool_kw)
self.__class__.http = http
self.firstrange = range_start, range_start + self.first_size - 1
self.data_queue = Queue.PriorityQueue()
self.range_queue = Queue.LifoQueue()
self._started_threads = {}
示例7: __init__
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def __init__(self, registry):
self.meta = _meta_
self.registry = registry
if self.meta.http_proxy:
self.http = urllib3.ProxyManager(self.meta.http_proxy)
else:
self.http = urllib3.PoolManager()
示例8: __init__
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def __init__(self, link):
self.link = link
self.meta = _meta_
self.red = _meta_.color["RED"]
self.endc = _meta_.color["ENDC"]
if self.meta.http_proxy:
self.http = urllib3.ProxyManager(self.meta.http_proxy)
else:
self.http = urllib3.PoolManager()
示例9: __proxy_pool
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def __proxy_pool(self):
"""
Create Proxy connection pool
:raise ProxyRequestError
:return: urllib3.HTTPConnectionPool
"""
try:
self.__server = self.__cfg.proxy if True is self.__cfg.is_standalone_proxy else self.__get_random_proxy()
if self.__get_proxy_type(self.__server) == 'socks':
disable_warnings(InsecureRequestWarning)
if not hasattr(self, '__pm'):
package_module = importlib.import_module('urllib3.contrib.socks')
self.__pm = getattr(package_module, 'SOCKSProxyManager')
pool = self.__pm(self.__server,
num_pools=self.__cfg.threads,
timeout=Timeout(self.__cfg.timeout,
read=self.__cfg.timeout),
block=True)
else:
pool = ProxyManager(self.__server,
num_pools=self.__cfg.threads,
timeout=Timeout(self.__cfg.timeout, read=self.__cfg.timeout),
block=True)
return pool
except (DependencyWarning, ProxySchemeUnknown, ImportError) as error:
raise ProxyRequestError(error)
示例10: ProxyManager
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def ProxyManager():
import urllib3
proxy = urllib3.ProxyManager('http://localhost:8500/')
return proxy
示例11: _build_http_manager
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def _build_http_manager(self):
pool_kwargs = {
'cert_reqs': 'CERT_REQUIRED',
'ca_certs': certifi and certifi.where() or None
}
proxy_url = utils.get_proxy_url(self.url)
if proxy_url:
return urllib3.ProxyManager(proxy_url, **pool_kwargs)
else:
return urllib3.PoolManager(**pool_kwargs)
示例12: _init_paw_telepot
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def _init_paw_telepot(self):
# You can leave this bit out if you're using a paid PythonAnywhere
# account
proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
"default": urllib3.ProxyManager(proxy_url = proxy_url, num_pools = 3,
maxsize = 10, retries = False, timeout = 30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager,
dict(proxy_url = proxy_url, num_pools = 1, maxsize = 1,
retries = False, timeout = 30))
# end of the stuff that's only needed for free accounts
示例13: _init_http_proxy
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def _init_http_proxy(self, http_proxy, **kwargs):
pool_options = dict(kwargs)
p = urlparse.urlparse(http_proxy)
scheme = p.scheme
netloc = p.netloc
if "@" in netloc:
auth, netloc = netloc.split("@", 2)
pool_options["proxy_headers"] = urllib3.make_headers(proxy_basic_auth=auth)
return urllib3.ProxyManager("%s://%s" % (scheme, netloc), **pool_options)
示例14: test_http_proxy_from_environ
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def test_http_proxy_from_environ():
os.environ["HTTP_PROXY"] = "proxy1.example.com:8080"
td = api.API("apikey")
assert isinstance(td.http, urllib3.ProxyManager)
assert td.http.proxy.url == "http://proxy1.example.com:8080"
assert td.http.proxy_headers == {}
示例15: test_http_proxy_from_keyword
# 需要导入模块: import urllib3 [as 别名]
# 或者: from urllib3 import ProxyManager [as 别名]
def test_http_proxy_from_keyword():
td = api.API("apikey", http_proxy="proxy2.example.com:8080")
assert isinstance(td.http, urllib3.ProxyManager)
assert td.http.proxy.url == "http://proxy2.example.com:8080"
assert td.http.proxy_headers == {}