本文整理匯總了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(' ')