1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import pycurl import io import wechat_sms import time def check_status(url): ''' 检查网站状态 :param url: :return: 异常信息,正常则不返回 ''' c = pycurl.Curl() #pycurl 实例 c.setopt(pycurl.URL, url) c.setopt(pycurl.TIMEOUT,10) #超时时间 b = io.BytesIO() c.setopt(pycurl.WRITEFUNCTION, b.write) c.perform() code = c.getinfo(pycurl.HTTP_CODE) #状态码 total_time = '%.2f'%c.getinfo(pycurl.TOTAL_TIME) #总时间 dns = '%.2f'%c.getinfo(pycurl.NAMELOOKUP_TIME) #DNS解析消耗 connet_time = '%.2f'%c.getinfo(pycurl.CONNECT_TIME) #建立连接时间 PRETRANSFER_TIME = '%.2f'%c.getinfo(pycurl.PRETRANSFER_TIME) #建立连接到准备传输数据时间 STARTTRANSFER_TIME = '%.2f'%c.getinfo(pycurl.STARTTRANSFER_TIME) #建立连接到传输开始消耗的时间 download = c.getinfo(pycurl.SPEED_DOWNLOAD) #平均下载速度 info = ('HTTP Code: %s\nTotal time: %s ms\nDNS lookup time: %s ms\nCONNECT_TIME: %s ms\nPRETRANSFER_TIME: %s ms\nSTARTTRANSFER_TIME: %s ms\nDownload Speed: %s byte/s' % (code, total_time, dns,connet_time,PRETRANSFER_TIME,STARTTRANSFER_TIME,download)) print(code) if code > 400 and connet_time > 10: #如果状态码大于400,或者消耗时间大于10秒,说明网站有问题,发送错误信息;否则不返回 return info else: return True def send_sms(url): ''' 发送信息 :param url: :return: ''' msm = check_status(url) project = '网站状态' #如果网站状态返回为True,则跳出,否则返回具体信息 while msm is True: print(msm) break else: print(msm) WeChat.sendMessage('medivh', project, msm) if __name__ == '__main__': WeChat = wechat_sms.WeChat('https://qyapi.weixin.qq.com/cgi-bin') #自定义网站列表,循环执行对网站的检测 url_list = ['http://wechat.xxx.com','http://www.xxx.com','http://xxx.com'] while True: for i in url_list: msm = send_sms(i) # 30秒检查一次 time.sleep(30)
|