【发布时间】:2015-08-17 19:11:30
【问题描述】:
我想编写一个 python 代码来检查网站是否启动并运行,如果我输入一个随机的东西,比如“whatever”,程序会说:
“该网站无效。请尝试另一个。”
如果服务器没有启动,它会让我能够检查另一个 URL,程序会说:
“网站服务器未启动。请尝试另一个。”
再次检查另一个 URL。 我尝试了这么久,总是得到一个我不想得到的异常,我想将异常更改为我选择的打印。
到目前为止我得到的是:
import urllib
print ("Which website would you like to check?")
website_url = raw_input("--> ")
#The user will stuck in a loop until a valid website is exist, using the Application protocol
while urllib.urlopen(website_url).getcode() != 200:
print ("The website does not exist. Which website would you like to check?")
website_url = raw_input("--> ")
感谢所有的帮助:)
【问题讨论】:
-
你写过这个程序吗?问题仅仅是您想更改错误消息吗?你能告诉我们你有什么吗?
-
@TigerhawkT3 刚刚做了 :)
-
您使用的是哪个 python 版本? Python 3?
-
@svfat 实际上我使用的是 PyCharm 我不确定 python 的版本,但我在我的电脑上安装了 python 2.7