我正在制作像isup.me这样的程序来检查网站是否已关闭。我试图让它打印出来"搜索..."直到它找到了网站的状态代码,但不知道如何做到这一点。有任何想法吗?
import requests
import time
website = input("Website to check: ")
if "http://" in website or "https://" in website:
pass
else:
website = "{0}{1}".format("http://", website)
def print_screen():
printing = "\nSearching...\n"
for letter in printing:
print(letter, end="")
time.sleep(.005)
print()
def requesting():
try:
user_agent = {'User-agent': 'Mozilla/5.0 (Windows NT 6.1)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
r = requests.get(website, timeout=10, headers = user_agent)
print("Website status code: ", r.status_code)
status = r.status_code
while True:
if status in range(400,599):
if status in range(403,404):
print("Website is up and running!")
break
else:
print("Website is down :( ")
break
else:
print("Website is up and running!")
break
except requests.exceptions.ConnectTimeout:
print("Website timed out, either your connection is extremely slow, or the website is down")
def main():
print_screen()
requesting()
main()