0

Script works ok when receiving a HTTP 200. However as soon as I get an exception I receive a error. I know the website does not have a valid SSL cert installed and I've tried to requests.exceptions.RequestException to catch anything, but still throws me errors. I'll also get exceptions such as proxy error come up now and then, and requests.exceptions.RequestException would not catch it. I have also tried, under try: request.raise_for_status() and that still throws an error. Code is as follows:

def verify_ssl(proxy_info, target): 
   print('Attempting to verify SSL Cert on %s:443' % target)
   if proxy_info == None:
      response = requests.get('https://%s' % target)
   if proxy_info != None:
      response = requests.get('https://%s' % target, proxies=proxy_info)
   try:
      response
   except requests.exceptions.SSLError as g:
      print('SSL Verification Error %s' % g)
   return ('Successfully Verified SSL Cert: HTTP 200 received.\n')

Debug throws me this:

    Traceback (most recent call last):
  File "c:\Users\appleta\Desktop\ping.py", line 69, in <module>
    ssl_result = verify_ssl(proxy_info, target)
  File "c:\Users\appleta\Desktop\ping.py", line 37, in verify_ssl
    response = requests.get('https://%s' % target)
  File "C:\Python34\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Python34\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python34\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python34\lib\site-packages\requests\sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python34\lib\site-packages\requests\adapters.py", line 511, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='blabla.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)'),))

1 Answer 1

3

In python the expression:

response

can never yield an exception. You have to put both get call inside the try block instead:

def verify_ssl(proxy_info, target): 
   print('Attempting to verify SSL Cert on %s:443' % target)
   try:
       if proxy_info is None:
          response = requests.get('https://%s' % target)
       else:
          response = requests.get('https://%s' % target, proxies=proxy_info)
   except requests.exceptions.SSLError as g:
      print('SSL Verification Error %s' % g)
      return 'Got SSL error'
   return 'Successfully Verified SSL Cert: HTTP 200 received.\n'

Also: note that you are always returning the string Successfully Verified SSL Cert ... because even in case of error you only print the error message but then execution resumes. You probably want to return something different in the except block.

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic Giacomo, thank you so much for that. I assumed that as i specified response to request.get it would pull that in the try command, so I didn't even think that could have been the issue. :)

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.