Python implementation APAHCE Web site Log Analysis Example

Source: Internet
Author: User
Tags ip country log log website ip
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 ()

'''

'''

  • 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.