An example of script maintenance is a bit messy, just as an instance, demonstrating how to quickly use the tool to quickly achieve the goal:
Application: shell and python data interaction, data capturing, and Code Conversion
Copy codeThe Code is as follows:
# Coding: UTF-8
#! /Usr/bin/python
'''
Program Description: apache access. log analysis
Analyze the source of the website IP Address
Date:
Author: gyh9711
Program Description: application to: shell and python data interaction, data capturing, encoding conversion
'''
Import OS
Import json
Import httplib
Import codecs
LogFile = '/var/log/apache2/access. Log'
# Logs
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 the access. log File and convert it to a python Array
File. write (u "field Description: ip access times data ip country city isp number province region \ n ")
IpDb = []
For I in cmd ('''/usr/bin/awk '{print $1}' % s | sort | uniq-c ''' % LogFile ):
Ip = I. strip (). split ('')
IpDb. append (ip)
# Analyze IP address sources through APIS provided by taobao
For I in ipDb:
_ TmpD = getIpCountry (I [1])
# Format description: ip address access data region where the isp number of the ip address country city is located
Out = "% s" % (I [1]. ljust (20), I [0]. ljust (10), _ tmpD ['country']. ljust (20), _ tmpD ['city']. ljust (16), _ tmpD ['isp _ id']. ljust (16), _ tmpD ['region']. ljust (16), _ tmpD ['region']. ljust (16 ))
Print out
File. write ("% s \ n" % out)
Conn. close ()
File. close ()
'''
'''