Python-based apahce website log analysis example

Source: Internet
Author: User
Tags ip country website ip
This article mainly introduces the example of implementing log analysis for apahce website using python. if you need it, you can refer to the example of maintaining the script. it is written in disorder, just as an example, demonstrate how to quickly use the tool to quickly achieve the goal:
Application: shell and python data interaction, data capturing, and code conversion

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

'''

'''

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.