本文整理汇总了Python中requests.exceptions.ProxyError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ProxyError方法的具体用法?Python exceptions.ProxyError怎么用?Python exceptions.ProxyError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.exceptions
的用法示例。
在下文中一共展示了exceptions.ProxyError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: exception_handle
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def exception_handle(method):
"""Handle exception raised by requests library."""
def wrapper(*args, **kwargs):
try:
result = method(*args, **kwargs)
return result
except ProxyError:
LOG.exception('ProxyError when try to get %s.', args)
raise ProxyError('A proxy error occurred.')
except ConnectionException:
LOG.exception('ConnectionError when try to get %s.', args)
raise ConnectionException('DNS failure, refused connection, etc.')
except Timeout:
LOG.exception('Timeout when try to get %s', args)
raise Timeout('The request timed out.')
except RequestException:
LOG.exception('RequestException when try to get %s.', args)
raise RequestException('Please check out your network.')
return wrapper
示例2: test_neutron_exception_is_raised_on_any_request_error
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def test_neutron_exception_is_raised_on_any_request_error(self):
# timeout exception raises InfobloxTimeoutError
f = mock.Mock()
f.__name__ = 'mock'
f.side_effect = req_exc.Timeout
self.assertRaises(exceptions.InfobloxTimeoutError,
connector.reraise_neutron_exception(f))
# all other request exception raises InfobloxConnectionError
supported_exceptions = [req_exc.HTTPError,
req_exc.ConnectionError,
req_exc.ProxyError,
req_exc.SSLError,
req_exc.TooManyRedirects,
req_exc.InvalidURL]
for ex in supported_exceptions:
f.side_effect = ex
self.assertRaises(exceptions.InfobloxConnectionError,
connector.reraise_neutron_exception(f))
示例3: get_url
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def get_url(url):
headers['Referer'] = url
count = 0
while True:
count += 1
if count < settings['maxtries']:
proxy = get_proxy()
else:
proxy = None
try:
resp = request('get', url, headers=headers, proxies={'http': proxy})
return resp
except ProxyError:
if count > settings['maxtries']+2:
print('Exit: Can not get url.<@get_url>')
exit(1)
continue
示例4: get_url
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def get_url(url):
headers['Referer'] = url
count = 0
while True:
count += 1
if count < settings['maxtries']:
proxy = get_proxy()
else:
proxy = None
try:
resp = request('get', url, headers=headers, proxies={'http': proxy})
return resp
except ProxyError:
if count > settings['maxtries']+2:
print('Exit: Could not get url.<@get_url>')
exit(1)
continue
示例5: test_no_proxy_domain_fail
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def test_no_proxy_domain_fail(self, socks5_proxy):
instance = {'proxy': {'http': 'http://1.2.3.4:567', 'no_proxy': '.google.com,example.com,example,9'}}
init_config = {}
http = RequestsWrapper(instance, init_config)
# no_proxy not match: .google.com
# ".y.com" matches "x.y.com" but not "y.com"
with pytest.raises((ConnectTimeout, ProxyError)):
http.get('http://google.com', timeout=1)
# no_proxy not match: example or example.com
with pytest.raises((ConnectTimeout, ProxyError)):
http.get('http://notexample.com', timeout=1)
with pytest.raises((ConnectTimeout, ProxyError)):
http.get('http://example.org', timeout=1)
# no_proxy not match: 9
with pytest.raises((ConnectTimeout, ProxyError)):
http.get('http://127.0.0.99', timeout=1)
示例6: delete
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def delete(self):
"""Delete the Case Management Object.
If no id is present in the obj then returns immediately.
"""
if not self.id: # pragma: no cover
self.tcex.log.warning('A case without an ID cannot be deleted.')
return
url = f'{self.api_endpoint}/{self.id}'
r = None
try:
r = self.tcex.session.delete(url)
self.tcex.log.debug(
f'Method: ({r.request.method.upper()}), '
f'Status Code: {r.status_code}, '
f'URl: ({r.url})'
)
except (ConnectionError, ProxyError): # pragma: no cover
self.tcex.handle_error(
951, ['OPTIONS', 407, '{\"message\": \"Connection Error\"}', self.api_endpoint]
)
if len(r.content) < 5000:
self.tcex.log.debug(u'response text: {}'.format(r.text))
else: # pragma: no cover
self.tcex.log.debug(u'response text: (text to large to log)')
if not self.success(r):
err = r.text or r.reason
if r.status_code == 404:
self.tcex.handle_error(952, [r.request.method.upper(), r.status_code, err, r.url])
self.tcex.handle_error(950, [r.status_code, err, r.url])
return
示例7: properties
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def properties(self):
"""Return defined API properties for the current object."""
if self._properties is None:
try:
r = self.tcex.session.options(self.api_endpoint, params={'show': 'readOnly'})
if r.ok:
self._properties = r.json()
except (ConnectionError, ProxyError):
self.tcex.handle_error(
951, ['OPTIONS', 407, '{\"message\": \"Connection Error\"}', self.api_endpoint]
)
return self._properties
示例8: test_unreachable_proxy
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def test_unreachable_proxy(self, proxy_host):
session = requests.Session()
with pytest.raises(ProxyError):
session.get(arbitrary_url, proxies=proxy_parameter_for_requests('http://' + proxy_host))
示例9: test_timeout_proxy
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def test_timeout_proxy(self):
# Travis can refuse quickly, and trigger ProxyError instead.
session = requests.Session()
with pytest.raises(ConnectTimeout):
session.get(arbitrary_url, timeout=0.001, proxies=proxy_parameter_for_requests('http://localhost'))
示例10: test_bad_proxy_no_failover
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def test_bad_proxy_no_failover(self, proxy_host):
"""Verify that Requests returns ProxyError when given a non-existent proxy."""
sess = PACSession(pac=PACFile(proxy_pac_js_tpl % 'PROXY %s:80' % proxy_host))
with pytest.raises(ProxyError):
sess.get(arbitrary_url)
示例11: test_pac_failover
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def test_pac_failover(self):
"""First proxy raises error. Transparently fail over to second proxy."""
sess = PACSession(pac=PACFile(proxy_pac_js_tpl % 'PROXY a:80; PROXY b:80; DIRECT'))
def fake_request(method, url, proxies=None, **kwargs):
if proxies and proxies['http'] == 'http://a:80':
raise ProxyError()
with _patch_request_base(side_effect=fake_request) as request:
sess.get(arbitrary_url)
request.assert_has_calls([
get_call(arbitrary_url, 'http://a:80'),
get_call(arbitrary_url, 'http://b:80'),
])
示例12: test_pac_failover_to_direct
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def test_pac_failover_to_direct(self):
"""Proxy fails. Next in line is DIRECT keyword."""
sess = PACSession(pac=PACFile(proxy_pac_js))
def fake_request_reject_proxy(method, url, proxies=None, **kwargs):
if proxies and proxies['http'] is not None:
raise ProxyError()
with _patch_request_base(side_effect=fake_request_reject_proxy) as request:
sess.get(arbitrary_url)
request.assert_has_calls([
get_call(arbitrary_url, fake_proxy_url),
get_call(arbitrary_url, 'DIRECT'),
])
示例13: test_pac_no_failover_available_exc_case
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def test_pac_no_failover_available_exc_case(self):
"""Special case where proxy fails but there's no DIRECT fallback. Error should bubble up,
and all applicable proxies should be tried again in the next request. Proxy failure from exception."""
sess = PACSession(pac=PACFile(proxy_pac_js_tpl % 'PROXY a:80; PROXY b:80'))
for _ in range(2):
with _patch_request_base(side_effect=ProxyError()) as request, \
pytest.raises(ProxyError):
sess.get(arbitrary_url)
request.assert_has_calls([
get_call(arbitrary_url, 'http://a:80'),
get_call(arbitrary_url, 'http://b:80'),
])
示例14: default_proxy_fail_exception_filter
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def default_proxy_fail_exception_filter(req_exc):
return isinstance(req_exc, (ProxyError, ConnectTimeout))
示例15: main
# 需要导入模块: from requests import exceptions [as 别名]
# 或者: from requests.exceptions import ProxyError [as 别名]
def main():
count = 0
while True:
# print('第 ',count,' 次测试')
count = count + 1
try:
#请求不同的代理和headers
global headers,count_proxys
headers = {'User-Agent': ua.random}
count_proxys = get_count_proxys()
print('代理总数: ',count_proxys,' 当前所用的代理:',proxy,'\n',headers)
start_time = time.clock()
html = crawl('http://www.baidu.com', proxy)
end_time = time.clock()
print('代理连接时间: ',(str(end_time-start_time))[:4],' 秒')
if html.status_code==200:
print(html)
return count
break
elif count>=10:
print('抓取网页失败')
break
except (ChunkedEncodingError,ConnectionError,Timeout,UnboundLocalError,UnicodeError,ProxyError):
global proxy
proxy = get_proxy()
print('代理失败,更换代理','\n')
# print(' ')