The maintenance script is a bit messy, but as an example, demonstrates how to quickly use a tool to quickly achieve a goal:
Application to: Shell and Python data interaction, data capture, code conversion
Copy Code code as follows:
#coding: Utf-8
#!/usr/bin/python
'''
Program Description: Apache access.log log Analysis
Analyze access to Web site IP sources
Date: 2014-01-06 17:01
author:gyh9711
Program Description: Apply to: Shell and Python data interaction, data capture, code 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"
Analyze #将access. log file and convert to Python array
File.write (U) field Description: IP access number according to the location of the ISP number province in the IP country city \ 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])
#格式说明: Number of IP visits according to the area of the ISP number province in the IP country city
Out= "%s%s%s%s%s%s%s" (i[1].ljust), i[0].ljust, _tmpd[' country '].ljust ', _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 ()
'''
'''