[python]Check site availability and whether the site has https

I have a dev server which is running with http, however to minimise the need to change the code, I want to write a function to check.

Here’s an example:

import requests
from requests.exceptions import ConnectionError
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

def is_https(url: str) -> bool:
    retries = Retry(total=2,
                    status_forcelist=[500, 502, 503, 504])
    with requests.Session() as s:
        s.mount("https://", HTTPAdapter(max_retries=retries))
            s.get(url, verify=False, timeout=0.5)
            return True
        except ConnectionError:
            return False

if __name__ == "__main__":
    url = "https://www.google.com"
    print(f"Is {url} using https? {is_https(url)}")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s