Python implementation APAHCE Web site log Analysis sample _python

Source: Internet
Author: User
Tags ip country log log

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 ()

'''

'''

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.