检测空页面 只需要检测页面是否有 必有的元素即可。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import time
import socket
def verificateOmWeb():
url = "此处需要url替换"
headers = {
"Cookie": "此处需要Cookie替换;"
}
req = requests.get(url=url, headers=headers)
# 获取到网页的内容,是string类型
result = req.content
# 解析一下这个域名对应的ip地址
myaddr = socket.getaddrinfo("此处需要域名替换", 'http')
webIp = myaddr[0][4][0]
# 校验网页的string内容中有自己想要的值,为了防止页面为空
if ("此处需要 必填元素或必填文本 替换" in result):
print("web is success,页面不为空")
else:
print("web content is none,页面为空")
return webIp
def readFile():
omiphosts = [
"ip替换1 域名替换",
"ip替换2 域名替换",
"ip替换3 域名替换"
]
for omiphost in omiphosts:
file_data = ""
ip_data = ""
# 读的形式打开本地的Hosts文件,读到需要修改的host行,进行修改替换
with open("C:\Windows\System32\drivers\etc\hosts", "r") as f:
for line in f:
if "域名替换" in line:
line = line.replace(line, omiphost)
file_data += line
# 写的形式打开文件,将替换的语句写进去
with open("C:\Windows\System32\drivers\etc\hosts", "w") as f:
f.write(file_data)
# 检查写的Hosts是否写进去了
with open("C:\Windows\System32\drivers\etc\hosts", "r") as f:
fread = f.read()
if omiphost in fread:
print(omiphost + " :hosts switch sucess,成功写入Hosts")
else:
print(omiphost + " :hosts is not switch sucess,,写入Hosts失败")
time.sleep(10)
ip_data = verificateOmWeb()
# 检查获取网页的ip地址与hosts切换的ip地址是一样的。
if ip_data in omiphost:
print(ip_data + " web hosts sucess ip一样")
else:
print("web hosts is not equal your hosts ip不一样")
if __name__ == '__main__':
readFile()