通过 Python 检查网站是否正常

发布于 2024-08-15 13:46:35 字数 247 浏览 7 评论 0原文

使用python,如何检查网站是否已启动?根据我的阅读,我需要检查“HTTP HEAD”并查看状态代码“200 OK”,但该怎么做?

干杯

相关

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 ?

Cheers

Related

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

海螺姑娘 2024-08-22 13:46:35

您可以尝试使用 urllib 中的 getcode() 来执行此操作a>

import urllib.request

print(urllib.request.urlopen("https://www.stackoverflow.com").getcode())
200

对于 Python 2,使用

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

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

import urllib.request

print(urllib.request.urlopen("https://www.stackoverflow.com").getcode())
200

For Python 2, use

print urllib.urlopen("http://www.stackoverflow.com").getcode()
200
扎心 2024-08-22 13:46:35

我认为最简单的方法是使用 Requests 模块。

import requests

def url_ok(url):
    r = requests.head(url)
    return r.status_code == 200

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
把时间冻结 2024-08-22 13:46:35

您可以使用 httplib

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

打印

200 OK

当然,前提是 www.python.org< /code> 已启动。

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.

撑一把青伞 2024-08-22 13:46:35
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request("http://stackoverflow.com")
try:
    response = urlopen(req)
except HTTPError as e:
    print('The server couldn\'t fulfill the request.')
    print('Error code: ', e.code)
except URLError as e:
    print('We failed to reach a server.')
    print('Reason: ', e.reason)
else:
    print ('Website is working fine')

适用于 Python 3

from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request("http://stackoverflow.com")
try:
    response = urlopen(req)
except HTTPError as e:
    print('The server couldn\'t fulfill the request.')
    print('Error code: ', e.code)
except URLError as e:
    print('We failed to reach a server.')
    print('Reason: ', e.reason)
else:
    print ('Website is working fine')

Works on Python 3

羁拥 2024-08-22 13:46:35
import httplib
import socket
import re

def is_website_online(host):
    """ This function checks to see if a host name has a DNS entry by checking
        for socket info. If the website gets something in return, 
        we know it's available to DNS.
    """
    try:
        socket.gethostbyname(host)
    except socket.gaierror:
        return False
    else:
        return True


def is_page_available(host, path="/"):
    """ This function retreives the status code of a website by requesting
        HEAD data from the host. This means that it only requests the headers.
        If the host cannot be reached or something else goes wrong, it returns
        False.
    """
    try:
        conn = httplib.HTTPConnection(host)
        conn.request("HEAD", path)
        if re.match("^[23]\d\d$", str(conn.getresponse().status)):
            return True
    except StandardError:
        return None
import httplib
import socket
import re

def is_website_online(host):
    """ This function checks to see if a host name has a DNS entry by checking
        for socket info. If the website gets something in return, 
        we know it's available to DNS.
    """
    try:
        socket.gethostbyname(host)
    except socket.gaierror:
        return False
    else:
        return True


def is_page_available(host, path="/"):
    """ This function retreives the status code of a website by requesting
        HEAD data from the host. This means that it only requests the headers.
        If the host cannot be reached or something else goes wrong, it returns
        False.
    """
    try:
        conn = httplib.HTTPConnection(host)
        conn.request("HEAD", path)
        if re.match("^[23]\d\d$", str(conn.getresponse().status)):
            return True
    except StandardError:
        return None
没有你我更好 2024-08-22 13:46:35

我使用 requests 来实现这一点,这样就很简单而且干净。
您可以定义和调用新函数(通过电子邮件等通知),而不是打印函数。 Try- except 块是必不可少的,因为如果主机无法访问,那么它将引发很多异常,因此您需要捕获所有异常。

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}")

I use requests for this, then it is easy and clean.
Instead of print function you can define and call new function (notify via email etc.). Try-except block is essential, because if host is unreachable then it will rise a lot of exceptions so you need to catch them all.

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}")
请止步禁区 2024-08-22 13:46:35

您可以使用requests库来查找网站是否已启动,即状态代码200

import requests
url = "https://www.google.com"
page = requests.get(url)
print (page.status_code) 

>> 200

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
波浪屿的海角声 2024-08-22 13:46:35

HTTPConnection 对象标准库中的 >httplib 模块可能会为您解决问题。顺便说一句,如果您开始在 Python 中使用 HTTP 进行任何高级操作,请务必查看 httplib2< /代码>;这是一个很棒的图书馆。

The HTTPConnection object from the httplib module in the standard library will probably do the trick for you. BTW, if you start doing anything advanced with HTTP in Python, be sure to check out httplib2; it's a great library.

笑梦风尘 2024-08-22 13:46:35

如果服务器宕机,在 python 2.7 x86 windows 上 urllib 没有超时并且程序会死锁。所以使用urllib2

import urllib2
import socket

def check_url( url, timeout=5 ):
    try:
        return urllib2.urlopen(url,timeout=timeout).getcode() == 200
    except urllib2.URLError as e:
        return False
    except socket.timeout as e:
        print False


print check_url("http://google.fr")  #True 
print check_url("http://notexist.kc") #False     

If server if down, on python 2.7 x86 windows urllib have no timeout and program go to dead lock. So use urllib2

import urllib2
import socket

def check_url( url, timeout=5 ):
    try:
        return urllib2.urlopen(url,timeout=timeout).getcode() == 200
    except urllib2.URLError as e:
        return False
    except socket.timeout as e:
        print False


print check_url("http://google.fr")  #True 
print check_url("http://notexist.kc") #False     
凉月流沐 2024-08-22 13:46:35

在我看来,caisah的回答错过了您问题的一个重要部分,即处理服务器离线的问题。

尽管如此,使用 requests 是我最喜欢的选择,尽管是这样的:

import requests

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

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")
送舟行 2024-08-22 13:46:35

如果“up”只是指“服务器正在提供服务”,那么您可以使用 cURL,如果收到响应,则说明“服务器正在运行”。

我无法给你具体的建议,因为我不是 python 程序员,但是这里有一个 pycurl http:// /pycurl.sourceforge.net/

If by up, you simply mean "the server is serving", then you could use cURL, and if you get a response than it's up.

I can't give you specific advice because I'm not a python programmer, however here is a link to pycurl http://pycurl.sourceforge.net/.

扬花落满肩 2024-08-22 13:46:35

您好,此类可以使用此类对您的网页进行速度和速度测试:

 from urllib.request import urlopen
 from socket import socket
 import time


 def tcp_test(server_info):
     cpos = server_info.find(':')
     try:
         sock = socket()
         sock.connect((server_info[:cpos], int(server_info[cpos+1:])))
         sock.close
         return True
     except Exception as e:
         return False


 def http_test(server_info):
     try:
         # TODO : we can use this data after to find sub urls up or down    results
         startTime = time.time()
         data = urlopen(server_info).read()
         endTime = time.time()
         speed = endTime - startTime
         return {'status' : 'up', 'speed' : str(speed)}
     except Exception as e:
         return {'status' : 'down', 'speed' : str(-1)}


 def server_test(test_type, server_info):
     if test_type.lower() == 'tcp':
         return tcp_test(server_info)
     elif test_type.lower() == 'http':
         return http_test(server_info)

Hi this class can do speed and up test for your web page with this class:

 from urllib.request import urlopen
 from socket import socket
 import time


 def tcp_test(server_info):
     cpos = server_info.find(':')
     try:
         sock = socket()
         sock.connect((server_info[:cpos], int(server_info[cpos+1:])))
         sock.close
         return True
     except Exception as e:
         return False


 def http_test(server_info):
     try:
         # TODO : we can use this data after to find sub urls up or down    results
         startTime = time.time()
         data = urlopen(server_info).read()
         endTime = time.time()
         speed = endTime - startTime
         return {'status' : 'up', 'speed' : str(speed)}
     except Exception as e:
         return {'status' : 'down', 'speed' : str(-1)}


 def server_test(test_type, server_info):
     if test_type.lower() == 'tcp':
         return tcp_test(server_info)
     elif test_type.lower() == 'http':
         return http_test(server_info)
围归者 2024-08-22 13:46:35

请求httplib2 是不错的选择:

# 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

如果使用 Ansible,您可以使用 fetch_url 函数:

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

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
祁梦 2024-08-22 13:46:35

我的 2 美分

def getResponseCode(url):
conn = urllib.request.urlopen(url)
return conn.getcode()

if getResponseCode(url) != 200:
    print('Wrong URL')
else:
    print('Good URL')

my 2 cents

def getResponseCode(url):
conn = urllib.request.urlopen(url)
return conn.getcode()

if getResponseCode(url) != 200:
    print('Wrong URL')
else:
    print('Good URL')
挽你眉间 2024-08-22 13:46:35

这是我使用 PycURL验证器

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))

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))
优雅的叶子 2024-08-22 13:46:35

有时某些网站在某些国家/地区(例如伊朗)被禁止,您无法直接使用您的 IP 访问它们。因此,我在 此链接 中找到了更好的解决方案,使用 https://www.isitdownrightnow.com 通过世界上多个分布式服务器请求您的域。所以你可以使用这段代码:

import re
import requests

domain = 'your domain ex:a.com'
print(f"checking '{domain}' is up?")
isitdown_url = f'https://www.isitdownrightnow.com/check.php?domain={domain}'
r = requests.get(isitdown_url)
r_text = (r.text.lower().replace('</div>', ' '))
status = (re.compile(f'{domain} is (.*) (?:it is not|and reachable)').search(r_text).group(1).split(' ')[0])
if status == 'down':
    raise ValueError(f"'{domain}' is down in all the world!please request later!")

sometimes some sites are forbidden in some countries (like Iran) and you can't access them directly with your ip. So I found better solution in this link that checks your domain with https://www.isitdownrightnow.com that requests your domain with several distributed servers in the world. So you can use this code:

import re
import requests

domain = 'your domain ex:a.com'
print(f"checking '{domain}' is up?")
isitdown_url = f'https://www.isitdownrightnow.com/check.php?domain={domain}'
r = requests.get(isitdown_url)
r_text = (r.text.lower().replace('</div>', ' '))
status = (re.compile(f'{domain} is (.*) (?:it is not|and reachable)').search(r_text).group(1).split(' ')[0])
if status == 'down':
    raise ValueError(f"'{domain}' is down in all the world!please request later!")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文