新加:下面代码编码识别可以用第三方模块chardet 以及使用urlopen.info().get_content_charset()获得。
很多大型网站为了减轻压力,避免单点故障,使用了多个服务器(IP)地址来为单一网站提供服务。
其中包括使用DNS轮训来实现负载。
但是对于管理员而言,如何可以监控这些服务器那些是ok那些是有问题的呢?
下面脚本可以帮你实现。
import socket,urllib.parse,configparser,re,sys,http.client,time
#coding:gbk
#域名转换IP地址函数
def dom2ip(dname):
iplist=socket.gethostbyname_ex(dname)[2]
return iplist
#字典数据生成函数
def parse(url,key):
zhlist[key]={}
zhlist[key]['host']=a.hostname
zhlist[key]['path']=a.path
zhlist[key]['port']=a.port
zhlist[key]['query']=a.query
zhlist[key]['url']=a.geturl()
try:
zhlist[key]['ips']=dom2ip(a.hostname)
except Exception as err:
print('你的配置文件格式有错误,\n错误信息:',err,'\n程序已退出')
sys.exit(1)
#headers
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko)'
}
def getinfo(domain,ip,path='/',port=80):
try:
headers['Host']=domain
h=http.client.HTTPConnection(ip,port,timeout=8)
h.request('GET',path,headers=headers)
r=h.getresponse()
print('服务器返回状态',r.status)
print('服务器返回结果',r.reason)
if not r.getheader('Date') == None:
print('目标时间:',r.getheader('Date'))
if not r.getheader('Server') == None:
print('目标Web端:',r.getheader('Server'))
if not r.getheader('Last-Modified') == None:
print('目标网页上次修改时间:',r.getheader('Last-Modified'))
if not r.getheader('Expries') == None:
print('目标网页过期时间:',r.getheader('Expries'))
if not r.getheader('Content-Length') == None:
print('目标头内容返回:',r.getheader('Content-Length'),'字节')
if not r.getheader('Content-Encoding') == None:
print('目标返回编码:',r.getheader('Content-Encoding'))
if not r.getheader('Set-Cookie') == None:
print('目标返回Cookie:',r.getheader('Set-Cookie'))
#分析网页的字符集
a=r.getheader('Content-Type')
b=re.findall('charset=(.*)$',a,re.I)
for i in b:
charset=i
try:
charset
res=res.decode(charset,'ignore')
except:
res=r.read(2048)
strset=re.findall(b'charset=(.*?)"',res,re.I)
for i in strset:
charset=i.decode()
try:
charset
res=res.decode(charset,'ignore')
except:
strset=re.findall(b'charset="(.*?)',res,re.I)
for i in strset:
charset=i.decode()
try:
charset
res=res.decode(charset,'ignore')
except:
charset='utf8'#设置默认charset
res=res.decode(charset,'ignore')
print('目标脚本编码:',charset)
#显示Title
data=re.findall('(.*) ',res,re.I)
for i in data:
i=i.strip()
type(i)
print ('目标返回Title为',':',i)
try:
i
except:
print ('目标网页有问题,未返回Title')
#print('目标错误原因:',res)
#判断不同情况的返回
if r.status==302:
Location=r.getheader('Location')
print('注意:目标网页有转向,请修改配置文件从新定义监控页面,\n转向地址:',Location)
elif r.status==404:
print('注意:目标网页被删除,请修改配置文件从新定义监控页面')
else:
if r.status !=200:
print('网站其他问题,参考返回状态,适当修改配置文件后尝试')
except Exception as err:
print ('连接错误,详细信息:',err)
#创建配置实例
ini=configparser.ConfigParser()
#读配置文件
ini.read(sys.path[0]+'\yatere.ini')
#监控主机列表
host=ini.get('webserver','servers').split(';')
#删除对象
del ini
#对应列表
zhlist={}
#填充字典数据
print ('\n你设置的监控网址有',len(host),'个,分别是:\n')
for i in host:
a=urllib.parse.urlparse(i)
parse(i,a.hostname)
print (a.geturl())
print('\n','--'*20)
for i in zhlist:
print (i,'有',len(zhlist[i]['ips']),'个IP地址:')
for l in zhlist[i]['ips']:
print (l,end='\t')
print ('\n')
a=input('按回车键继续...')
#删除对象
del a,i,l,host
while True:
for domk in zhlist:
c=0
print('现在是',domk,'域的信息','---'*8,'\n\n')
for ipadd in zhlist[domk]['ips']:
c=c+1
print (domk,'的第',c,'台IP为',ipadd,'的服务器信息如下:')
getinfo(domk,ipadd,zhlist[domk]['path']+'?'+zhlist[domk]['query'])
print('\r')
time.sleep(5)
del domk,ipadd,c
ini文件:
;可以修改本配置文件实现相关自定义参数设置
;
;需要监控多网站,用;号隔开就可以了
[webserver]
servers = http://www.google.com;http://www.jjj.com/index.html
监控结果: