Exception Handling ProxyError, OSError in Python

Mister Source

I am trying to exception handle these proxy errors, i am trying following with no success:

from requests.exceptions import ProxyError, SSLError, ConnectionError

def adding(self):

    headers={
      'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36'
    }

    url = 'url'

    r = self.session.get(url, headers=headers)

def run(self):
    while True:
          try:
              self.adding()
          except OSError as e:
          print 'OSError'

          except ProxyError as g:
          print 'ProxyError'

However when i run python script i still get errors in the log:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "xx.py", line 273, in run
    self.adding()
  File "xx.py", line 107, in adding
    r = self.session.get(url, headers=headers)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 521, in get
    return self.request('GET', url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/adapters.py", line 502, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='url', port=443): Max retries exceeded with url: url (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 Bad Request',)))
python

Answers

answered 3 months ago Ctrl S #1

The print statements below your exceptions are not indented to be part of their respective except statements, and your try does not seem to be indented correctly. I'm not sure if that solves your problem or not, but it does appear to be unintentional.

comments powered by Disqus