The maintenance script is a bit confusing, just as an example of how to quickly use the tool to achieve the goal quickly:
Apply to: Shell and Python data interaction, data capture, encoding conversion
The code is as follows:
#coding: Utf-8
#!/usr/bin/python
'''
Program Description: Apache access.log log Analysis
Analysis of access to website IP sources
Date: 2014-01-06 17:01
author:gyh9711
Program Description: Apply to: Shell and Python data interaction, data capture, encoding conversion
'''
Import OS
Import JSON
Import Httplib
Import Codecs
Logfile= '/var/log/apache2/access.log '
#日志
logmess= '/tmp/acc.log '
If Os.path.isfile (logmess):
Os.system (' cp/dev/null%s '% logmess)
File=codecs.open (logmess, ' w+ ', encoding= ' utf-8 ')
def cmd (cmd):
return Os.popen (cmd). ReadLines ()
'''
def getip (IP):
Return Json.loads (Os.popen ("/usr/bin/curl http://ip.taobao.com/service/getIpInfo.php?ip=%s"% IP). ReadLine ()) [' Data ']
'''
conn = Httplib. Httpconnection (' ip.taobao.com ')
def getipcountry (IP):
Conn.request (' GET ', '/service/getipinfo.php?ip=%s '% IP)
R1=conn.getresponse ()
if R1.status = = 200:
Return Json.loads (R1.read ()) [' Data ']
Else
Return "Error"
#将access. log file for analysis and to Python array
File.write (U "field Description: IP access times according to IP country city's ISP number province area \ n")
Ipdb=[]
For i in cmd ('/usr/bin/awk ' {print $} '%s |sort |uniq-c "% LogFile):
ip = I.strip (). Split (")
Ipdb.append (IP)
#通过taobao provide interface analysis IP address source
For I in IpDb:
_tmpd=getipcountry (I[1])
#格式说明: IP access times According to the IP country city's ISP number province area
out= "%s%s%s%s%s%s%s"% (I[1].ljust), I[0].ljust (Ten), _tmpd[' Country '].ljust (a), _tmpd[' City '].ljust (+), _tmpd[' isp_id '].ljust (+), _tmpd[' region '].ljust (+), _tmpd[' area '].ljust (16))
Print out
File.write ("%s\n"%out)
Conn.close ()
File.close ()
'''
'''