我正在尝试检查 100 多个域是否已启动。 我使用 python,我将所有域都放在一个 csv 文件中,然后我逐行读取文件,我使用请求库来获取域的代码状态。这是剧本
import requests
import csv
import time
SLEEP = 0 # Time in seconds the script should wait between requests
url_list = []
url_statuscodes = []
url_statuscodes.append(["url","status_code"]) # set the file header for output
def getStatuscode(url):
try:
r = requests.head(url,verify=False,timeout=5)
return (r.status_code)
except:
return -1
# Url checks from file Input
# use one url per line that should be checked
with open('domainlist.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
url_list.append(row[0])
# Loop over full list
for url in url_list:
#print(url)
check = [url,getStatuscode(url)]
time.sleep(SLEEP)
url_statuscodes.append(check)
print(url_statuscodes.append(check)) #This line is my problem, I get "None" when I print statuscodes, I want to get something like 200 if the domaine is up
# Save file
with open("domainstatus.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(url_statuscodes)
我想在打印时得到
url_statuscodes.append(check)
200,这意味着域已启动。而不是 200 我在打印时得到“无”。如果域名不可用,我想得到 404 或其他类似域名已关闭的东西。
提前致谢!