-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_code.py
More file actions
105 lines (97 loc) · 3.21 KB
/
http_code.py
File metadata and controls
105 lines (97 loc) · 3.21 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import os
import re
import sys
import time
import StringIO
import pycurl
#import smtplib
#from email import encoders
#from email.header import Header
#from email.mime.text import MIMEText
#from email.utils import parseaddr, formataddr
from http_url import Http_url as url
from dingtalkchatbot.chatbot import DingtalkChatbot
#def _format_addr(s):
# name, addr = parseaddr(s)
# return formataddr(( \
# Header(name, 'utf-8').encode(), \
# addr.encode('utf-8') if isinstance(addr, unicode) \
#else addr))
#
#def mail(messg):
# from_addr='user@shinetour.com'#raw_input('From: ')
# password='pass'#raw_input('Password: ')
# to_addr='email_addr@139.com'#raw_input('To: ')
# smtp_server='smtp.exmail.qq.com'#raw_input('SMTP server: ')
#
# msg = MIMEText(messg, 'plain', 'utf-8')
# msg['From'] = _format_addr(u'Http Status Code <%s>' \
# % from_addr)
# msg['To'] = _format_addr(u'admin<%s>' % to_addr)
# msg['Subject'] = Header(u'http code warning!!! ',\
#'utf-8').encode()
# server = smtplib.SMTP(smtp_server, 25)
# server.set_debuglevel(1)
# server.login(from_addr, password)
# server.sendmail(from_addr, [to_addr], msg.as_string())
# server.quit()
class urlpass:
def __init__(self):
self.contents = ''
def body_callback(self,buf):
self.contents = self.contents + buf
def urlgzip(input_url):
try:
t = urlpass()
c = pycurl.Curl()
c.setopt(pycurl.WRITEFUNCTION,t.body_callback)
c.setopt(pycurl.ENCODING, 'gzip')
c.setopt(pycurl.URL,input_url)
c.perform()
http_code = c.getinfo(pycurl.HTTP_CODE)
http_conn_time = c.getinfo(pycurl.CONNECT_TIME)
http_pre_tran = c.getinfo(pycurl.PRETRANSFER_TIME)
http_start_tran = c.getinfo(pycurl.STARTTRANSFER_TIME)
http_total_time = c.getinfo(pycurl.TOTAL_TIME)
http_size = c.getinfo(pycurl.SIZE_DOWNLOAD)
return "http_code:%d,http_size:%d,conn_time:%.1f,\
pre_tran:%.1f,start_tran:%.1f,total_time:%.1f" % \
(http_code,http_size,http_conn_time,http_pre_tran, \
http_start_tran,http_total_time)
except:
print('URL Error: %s') % input_url
#sys.exit(1)
def DingDing(ddmesg):
xianding=DingtalkChatbot(url.rob_url)
xianding.send_text(msg=ddmesg, is_at_all=False)
def warning_rule(args):
try:
urlinfo=urlgzip(args)
total_time=int(float(urlinfo.split(',')[-1].\
split(':')[-1]))
http_Code=int(urlinfo.split(',')[0].split(':')[1])
Message=("url:%s\nhttp_code:%s\ntotal_time:%.1f\n")\
%(args,http_Code,total_time)
#print(Message)
if total_time >= 10 or http_Code >= 400:
#mail(Message)
DingDing(Message)
except:
print('URL Error: %s') % args
if __name__ == '__main__':
while True:
for lineW in url.web:
warning_rule(lineW)
#print("sleep: %s") % url.sleep_time
continue
time.sleep(url.sleep_time/10)
for lineI in url.interface:
warning_rule(lineI)
continue
time.sleep(url.sleep_time/10)
for lineM in url.middle:
warning_rule(lineM)
continue
time.sleep(url.sleep_time)