python check if website is reachable 1

python check if website is reachable

import requests

URL = "https://api.github.com"

try:
    response = requests.head(URL)
except Exception as e:
    print(f"NOT OK: {str(e)}")
else:
    if response.status_code == 200:
        print("OK")
    else:
        print(f"NOT OK: HTTP response code {response.status_code}")

Here is what the above code is Doing:
1. It’s making a HEAD request to the URL.
2. If the request is successful, it’s checking the HTTP response code.
3. If the response code is 200, it’s printing “OK”.
4. If the response code is not 200, it’s printing “NOT OK” and the HTTP response code.
5. If the request is not successful, it’s printing “NOT OK” and the error message.

Similar Posts