-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_file_mysql.py
More file actions
75 lines (61 loc) · 1.92 KB
/
read_file_mysql.py
File metadata and controls
75 lines (61 loc) · 1.92 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
# !/usr/bin/env python
# -*- coding:utf-8 -*-
"""
read from file by line, flow data!
usage: give a filename, and start line
fileinfo(filename, startline)
"""
import record_err
import attack_deliver
import linecache
import commands
import time
import json
import datetime
import read_file_mysql_delsame
def readfile(content):
# change str to dict
try:
# print content, type(content)
dict_content = eval(content)
if dict_content[u'hostname'] != u'www.jiedaibao.com':
read_file_mysql_delsame.catagory(dict_content)
except Exception as e:
record_err.logrecord()
def filename(logfile, file_count, startline):
try:
# 控制读取次数
for i in range(file_count-startline+1):
content = linecache.getline(logfile, startline)
readfile(content)
startline += 1
# 已处理到多少行
linecache.clearcache()
return startline - 1
except Exception as e:
record_err.logrecord()
def filecount(logfile):
try:
stats, output = commands.getstatusoutput('wc -l %s' % (logfile))
count = int(output.split()[0])
return count
except Exception as e:
record_err.logrecord()
def fileinfo(logfile, start_line=1):
try:
# 获取文件行数
file_count = filecount(logfile)
processed_line = filename(logfile, file_count, start_line)
while 1:
file_record = open('/tmp/file_no.txt', 'a')
file_count = filecount(logfile)
if file_count > processed_line:
processed_line = filename(logfile, file_count, processed_line+1)
# record the last processed line number
file_record.write(str(processed_line))
file_record.close()
time.sleep(3)
except Exception as e:
record_err.logrecord()
if __name__ == '__main__':
fileinfo('/tmp/attackinfo.txt', 1)