Checking if a website is up via Python

后端 未结 14 1998
南笙
南笙 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:09

    I think the easiest way to do it is by using Requests module.

    import requests
    
    def url_ok(url):
        r = requests.head(url)
        return r.status_code == 200
    
    0 讨论(0)
  • 2020-12-04 10:12

    You can use httplib

    import httplib
    conn = httplib.HTTPConnection("www.python.org")
    conn.request("HEAD", "/")
    r1 = conn.getresponse()
    print r1.status, r1.reason
    

    prints

    200 OK
    

    Of course, only if www.python.org is up.

    0 讨论(0)
  • 2020-12-04 10:14

    Here's my solution using PycURL and validators

    import pycurl, validators
    
    
    def url_exists(url):
        """
        Check if the given URL really exists
        :param url: str
        :return: bool
        """
        if validators.url(url):
            c = pycurl.Curl()
            c.setopt(pycurl.NOBODY, True)
            c.setopt(pycurl.FOLLOWLOCATION, False)
            c.setopt(pycurl.CONNECTTIMEOUT, 10)
            c.setopt(pycurl.TIMEOUT, 10)
            c.setopt(pycurl.COOKIEFILE, '')
            c.setopt(pycurl.URL, url)
            try:
                c.perform()
                response_code = c.getinfo(pycurl.RESPONSE_CODE)
                c.close()
                return True if response_code < 400 else False
            except pycurl.error as err:
                errno, errstr = err
                raise OSError('An error occurred: {}'.format(errstr))
        else:
            raise ValueError('"{}" is not a valid url'.format(url))
    
    0 讨论(0)
  • 2020-12-04 10:15

    Requests and httplib2 are great options:

    # Using requests.
    import requests
    request = requests.get(value)
    if request.status_code == 200:
        return True
    return False
    
    # Using httplib2.
    import httplib2
    
    try:
        http = httplib2.Http()
        response = http.request(value, 'HEAD')
    
        if int(response[0]['status']) == 200:
            return True
    except:
        pass
    return False
    

    If using Ansible, you can use the fetch_url function:

    from ansible.module_utils.basic import AnsibleModule
    from ansible.module_utils.urls import fetch_url
    
    module = AnsibleModule(
        dict(),
        supports_check_mode=True)
    
    try:
        response, info = fetch_url(module, url)
        if info['status'] == 200:
            return True
    
    except Exception:
        pass
    
    return False
    
    0 讨论(0)
  • 2020-12-04 10:17

    You could try to do this with getcode() from urllib

    >>> print urllib.urlopen("http://www.stackoverflow.com").getcode()
    >>> 200
    

    EDIT: For more modern python, i.e. python3, use:

    import urllib.request
    print(urllib.request.urlopen("http://www.stackoverflow.com").getcode())
    >>> 200
    
    0 讨论(0)
  • 2020-12-04 10:17

    You may use requests library to find if website is up i.e. status code as 200

    import requests
    url = "https://www.google.com"
    page = requests.get(url)
    print (page.status_code) 
    
    >> 200
    
    0 讨论(0)
提交回复
热议问题