python writes the content output of the regular filter to an instance of a file
- 2021-01-02 21:54:15
- OfStack
Process filter Apache log files
access_test.log file contents
27.19.74.143 - - [30/May/2015:17:38:21 +0800] "GET /static/image/smiley/default/sleepy.gif HTTP/1.1" 200 2375
8.35.201.164 - - [30/May/2015:17:38:21 +0800] "GET /static/image/common/pn.png HTTP/1.1" 200 592
Filter target
60.166.12.170 31/May/2013:00:00:02 /forum.php 200 45780
After processing, write the contents to file 20160205.txt
#!/usr/bin/env python
# - coding:utf - 8 -*-
import re,sys
with open('access_test.log') as f:
for line in f:
parseip = re.search(r'(.*?) - - ', line)
parsetime = re.search(r'
(.∗?)
(.∗?)
', line)
parseurl = re.search(r' "\w+ (.*?) HTTP/', line)
parsestatus = re.search(r' HTTP/(.*?)" (.*?) ', line)
parseTraffic = re.search(r'\d+ \d+', line)
if parseip and parsetime and parseurl and parsestatus and parseTraffic is None:
continue
output=sys.stdout
outputfile=open('20160205.txt','a')
sys.stdout=outputfile
print parseip.group(1).split('?')[0] + '\t' + parsetime.group(1).split('?')[0] + '\t' + parseurl.group(1).split('?')[0] + '\t' + parsestatus.group(2) + '\t' + parseTraffic.group(0).split(' ')[1]
outputfile.close()
sys.stdout=output
import sys
Then add the following code before the code to write the output data to the file
output=sys.stdout
outputfile=open(filename,'w')
sys.stdout=outputfile
filename above represents the output file
Add the following code when the program ends or returns to normal output
outputfile.close()
sys.stdout=output
Restore the output to the normal output value you started saving