This repository was archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.py
83 lines (75 loc) · 2.03 KB
/
clean.py
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
#!/usr/bin/python
# encoding:utf-8
import urllib
import urllib2
import os
import sys
import logging
import logging.config
import json
reload(sys)
sys.setdefaultencoding('utf8')
os.chdir(sys.path[0])
logging.config.fileConfig("logging_monitor.conf") # 采用配置文件
logger = logging.getLogger('clean')
# 自定义的异常类
class NetworkError(Exception):
tp = 0
name = ""
def __init__(self, arg1, arg2):
self.tp = arg1
self.name = arg2
ErrorType = {0:'Database Connection Error',1:'Database Query Error',2:'Database Modification Error',
3:'File Deletion Error',4:'File Modification Error',5:'IP Conflict Error',6:'Invalid FIB Error',7:'Invalid Info File Error'}
def sendpost(url, data):
data_urlencode = urllib.urlencode(data)
req = urllib2.Request(url=url, data=data_urlencode)
res_data = urllib2.urlopen(req)
res = res_data.read()
fail = json.loads(res)
if fail['Fail'] == 1:
raise NetworkError(fail['Type'],os.path.basename(url))
elif fail['Fail'] == 0:
logger.info(fail['content'])
return
now=0
total=10
# 发给clean.php的部分
while True:
try:
now = now + 1
alive_data = {'event': 'clean','id':-1}
alive_url = "http://localhost/clean.php"
sendpost(alive_url, alive_data)
break
except NetworkError, e:
logger.error('In '+str(e.name)+": "+ErrorType[e.tp])
if now >=total:
logger.critical('Fail to clean, have retried %d times' % total)
break
else:
continue
except urllib2.HTTPError, e:
logger.error('HTTP Error[%s]' % str(e.code))
if now >=total:
logger.critical('Fail to clean, have retried %d times' % total)
break
else:
continue
except urllib2.URLError, e:
logger.error('URL Error: '+str(e.reason))
if now >=total:
logger.critical('Fail to clean, have retried %d times' % total)
break
else:
continue
except KeyboardInterrupt:
logger.warning('Interrupt by Keyboard [CTRL + C]')
break
except Exception,e:
logger.error('Other Error: '+str(e))
if now >=total:
logger.critical('Fail to clean, have retried %d times' % total)
break
else:
continue