The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Webserver log script
Discuss Webserver log script in the Python Programming forum on Dev Shed. Webserver log script Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 18th, 2004, 06:55 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Location: UK
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Webserver log script
Hi all i'm new to all this so i apologise. I've looked at tutorials for python and i can't make heads nor tails of them.
I need to make a python program which will summarise an apache web server log file. It should report the total number of successful web server accesses.
|

January 18th, 2004, 10:27 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Adapting some code from the Python Cookbook (original author for this snippet was Mark Nenadov):
Code:
#!/usr/bin/env python
def CalculateApacheIpHits(logfile_pathname):
IpHitListing = {}
# Use readlines(), if xreadlines() doesn't work.
Contents = open(logfile_pathname, "r").xreadlines()
# Alternatively, use the following code, if you don't have
# xreadlines and don't want to slurp the code with readlines.
# Comment the "Contents = open..." and "for line in..." and
# replace with the lines below:
#input = open(logfile_pathname, "r")
#while 1:
# line = input.readline()
# if not line: break
for line in Contents:
#Split the string to isolate the IP address
Ip = line.split(" ")[0]
if 6 < len(Ip) <= 15:
#Update the hash
IpHitListing[Ip] = IpHitListing.get(Ip, 0) + 1;
return IpHitListing
if __name__ == "__main__":
# Substitute your own log file name here
hits = CalculateApacheIpHits("/var/log/apache/access_log");
for ip in hits.keys():
print ip, "\t\t", hits[ip]
This one only summarises the hits by IP address. However, you can also get it to check the HTTP return code (if you're logging that, of course) and only log if a successful code (i.e. 200, 301, 302 etc.) is returned.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
Last edited by Scorpions4ever : January 18th, 2004 at 10:32 AM.
|

January 18th, 2004, 11:43 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Location: UK
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thank you very much....this will help me tremendously. Will apply it monday hopefully.
Words can't explain.
|

January 18th, 2004, 01:25 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Just a small, more up to date example of how this task could be done...
Code:
#!/usr/bin/env python
def hits(path):
count = {}
codes = ('200', '301', '302', 'etc')
for line in file(path, 'r'):
line = line.split()
if line[-2] in codes: count[len(count) + 1] = value
return count
This should do what scorpy was discribing (untested). This actually has a lot of potensial  , would be interested in seeing you're finished piece if you're willing to show it.
Note: xreadlines was depreciated in Python 2.3 and replaced with 'for line in file'
Anyway, have fun.
Mark.
__________________
programming language development: www.netytan.com – Hula
|

January 19th, 2004, 10:57 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Location: UK
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Cool thx
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|