July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Mark Nenadov
You need to examine a log file from Apache to know the number of hits recorded from each individual IP address that accessed it.
Many of the chores of administering a web server have to do with analyzing Apache logs, which Python makes easy:
def CalculateApacheIpHits(logfile_pathname):
# Make a dictionary to store IP addresses and their hit counts
# and read the contents of the log file line by line
IpHitListing = {}
Contents = open(logfile_pathname, "r").xreadlines( )
# You can use .readlines in old Python, but if the log is huge...
# Go through each line of the logfile
for line in Contents:
# Split the string to isolate the IP address
Ip = line.split(" ")[0]
# Ensure length of the IP address is proper (see discussion)
if 6 < len(Ip) <= 15:
# Increase by 1 if IP exists; else set hit count = 1
IpHitListing[Ip] = IpHitListing.get(Ip, 0) + 1
return IpHitListingThis recipe shows a function that returns a dictionary containing the hit counts for each individual IP address that has accessed your Apache web server, as recorded in an Apache log file. For example, a typical use would be:
HitsDictionary = CalculateApacheIpHits("/usr/local/nusphere/apache/logs/access_log")
print HitsDictionary["127.0.0.1"]This function is quite useful for many things. For example, I often use it in my code to determine the number of hits that are actually originating from locations other than my local host. This function is ...
Read now
Unlock full access