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, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504]) with requests.Session() as s: s.mount("https://", HTTPAdapter(max_retries=retries)) try: 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)}")