如何创建检查网站可用性的python函数?

时间:2019-02-21 15:16:00

标签: python function

我当前编写的代码如下:

def Ws_availbality(url):
    check = print(urllib.request.urlopen("https://www.youtube.com/").getcode())
    if check == 200:
        print ("Website is running")
    else:
        print("The website is currently down")

Ws_availbality('https://www.youtube.com/')

问题是尽管返回200,但我无法执行if语句。它只是执行else语句。我也对不同的功能方法持开放态度。

1 个答案:

答案 0 :(得分:0)

您必须将打印内容移到其他地方:

def Ws_availbality(url):
    check = urllib.request.urlopen("https://www.youtube.com/").getcode()
    print(check)
    if check == 200:
        print ("Website is running")
    else:
        print("The website is currently down")

Ws_availbality('https://www.youtube.com/')