Python-based apahce website log analysis example

Source: Internet
Author: User
Tags ip country website ip

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

'''

'''

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.