Python requests proxy connection timeout -
i using code check proxy servers:
def check_proxy(p): try: r = requests.get('https://httpbin.org/get', proxies={'https': 'https://%s' % p}, timeout=5) if r.status_code == 200: return true else: return false except: return false
it works fine working proxy. fails bad proxy after several minutes:
traceback (most recent call last): file "/home/me/desktop/general/test_proxyt.py", line 22, in check_proxy r = requests.get('https://httpbin.org/get', proxies={'https': 'https://%s' % p}, timeout=5) file "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 59, in return request('get', url, **kwargs) file "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 48, in request return session.request(method=method, url=url, **kwargs) file "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 451, in request resp = self.send(prep, **send_kwargs) file "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 557, in send r = adapter.send(request, **kwargs) file "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 413, in send raise connectionerror(e, request=request) connectionerror: httpsconnectionpool(host='httpbin.org', port=443): max retries exceeded url: /get (caused proxyerror('cannot connect proxy.', error(110, 'connection timed out')))
is possible set proxy connection timeout exception?
may can try exceptions
come module.
def check_proxy(p): try: r = requests.get('https://httpbin.org/get', proxies={'https': 'https://%s' % p}, timeout=5) if r.status_code == 200: return true else: return false except requests.exceptions.timeout e: # maybe set retry print e except requests.exceptions.requestexception e: print e
Comments
Post a Comment