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:
import requests
try:
response = str(requests.get(url, verify=True, allow_redirects=True))
print(response)
if "SSL: CERTIFICATE_VERIFY_FAILED" in response:
print("url \"" + url + "\" has an invalid SSL certificate")
else:
valid = True
for command in commands:
if command.title == "get info":
if command.expirationDate > datetime.date:
print("url has an expired SSL certificate.")
valid = False
if valid:
print("url \"" + url + "\" has a valid SSL certificate")
except requests.exceptions.InvalidURL:
print("Invalid url \"" + url + "\"")
except requests.exceptions.MissingSchema:
print("Invalid url \"" + url + "\"")
except (socket.gaierror, urllib3.exceptions.NewConnectionError, urllib3.exceptions.MaxRetryError, requests.exceptions.ConnectionError):
print("Could not resolve url \"" + url + "\" to host")
Putting in the URL "https://expired.badssl.com/ returns
Could not resolve url "https://expired.badssl.com/" to host
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.