Checking if a website is up via Python

后端 未结 14 2064
南笙
南笙 2020-12-04 09:27

By using python, how can I check if a website is up? From what I read, I need to check the "HTTP HEAD" and see status code "200 OK", but how to do so ?

14条回答
  •  情书的邮戳
    2020-12-04 10:23

    In my opinion, caisah's answer misses an important part of your question, namely dealing with the server being offline.

    Still, using requests is my favorite option, albeit as such:

    import requests
    
    try:
        requests.get(url)
    except requests.exceptions.ConnectionError:
        print(f"URL {url} not reachable")
    

提交回复
热议问题