2

I am using this code to 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 with working proxy. it fails with 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 get
    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 with url: /get (Caused by ProxyError('Cannot connect to proxy.', error(110, 'Connection timed out')))

Is it possible to set proxy connection timeout exception?

2
  • Did you try the exceptions? Commented Jun 1, 2015 at 9:02
  • 1
    probably need to write HTTPAdapter, for proxy is handled by urllib3 Commented Jun 1, 2015 at 10:08

2 Answers 2

3

May be you can try exceptions that come with the 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 as e:

        # Maybe set up for a retry
        print e

    except requests.exceptions.RequestException as e:
        print e
Sign up to request clarification or add additional context in comments.

Comments

0

To close the circle here, this was a bug in requests that has since been fixed by updating to urllib3

2 Comments

The link is dead and I'm having this issue
Commenting here because I am having a similar problem with httpx, and am doing research on how to fix this problem. Disheartening that the link is dead and that this answer is so old =\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.