title: How do I actually verify a given url's SSL certificate?
body: <p>I am writing a piece of code to verify that a given URL's SSL certificate is valid, however, the requests library does not seem to be working correctly. Here is the code:</p>
  <pre><code>import requests
      try:
          response = str(requests.get(url, verify=True, allow_redirects=True))
          print(response)
          if &quot;SSL: CERTIFICATE_VERIFY_FAILED&quot; in response:
              print(&quot;url \&quot;&quot; + url + &quot;\&quot; has an invalid SSL certificate&quot;)
          else:
              valid = True
              for command in commands:
                  if command.title == &quot;get info&quot;:
                      if command.expirationDate &gt; datetime.date:
                          print(&quot;url has an expired SSL certificate.&quot;)
                          valid = False
              if valid:
                  print(&quot;url \&quot;&quot; + url + &quot;\&quot; has a valid SSL certificate&quot;)
      except requests.exceptions.InvalidURL:
          print(&quot;Invalid url \&quot;&quot; + url + &quot;\&quot;&quot;)
      except requests.exceptions.MissingSchema:
          print(&quot;Invalid url \&quot;&quot; + url + &quot;\&quot;&quot;)
      except (socket.gaierror, urllib3.exceptions.NewConnectionError, urllib3.exceptions.MaxRetryError, requests.exceptions.ConnectionError):
          print(&quot;Could not resolve url \&quot;&quot; + url + &quot;\&quot; to host&quot;)
  </code></pre>
  <p>Putting in the URL &quot;https://expired.badssl.com/ returns
  <code>Could not resolve url &quot;https://expired.badssl.com/&quot; to host</code>
  However, I can in fact navigate to this page.
  Furthermore, pages without a valid certificate are showing as valid. One of my friends ran this exact code on his beef-xss server, and the output was that it had a valid certificate. I've looked at many tutorials which say the same thing, however, I must be missing something given how nothing I have tried is working.</p>