Approximate session data from apache access.log - python
<p>How might one use the ip and timestamp from Apache's access log to approximate a "session" for a given visitor?  A session would include all consecutive requests within a given period, say 60secs.</p>

<p>I have a class to parse the log file, and follow an IP address through it (the log is in timestamp order, thankfully).  The class creates a tuple of dictionaries, which contain the various log fields and a python datetime object for the access timestamp.</p>

<pre><code>class ApacheLogParser(object):
    def __init__(self, file):
        self.lines = __parse(file)
    def __parse(self, file):
        """ use a regex to parse the file
            return a tuple of dictionaries
        """
    def follow_ip(self, ip):
        """ all entries for a given ip, in order of appearance in the log """
        return (line for line in self.lines if re.search(ip, line['ip']))

log = ApacheLogParser('access.log')
for line in log.follow_ip('1.2.3.4'):
    print "%s %s" % (line['path'], line['datetime'].date())
</code></pre>

<p>How might I add functionality to this class to be able to iterate through these approximated "sessions"?</p>

<p>Thanks!</p>

<p>EDIT:
While forming my edit, I came up with this:</p>

<pre><code>ip = '1.2.3.4'
ipdata = list(log.track_ip(ip))
initial_dt = ipdata[0]['datetime']
sess = [x for x in ipdata if x['datetime'] &lt; initial_dt + datetime.timedelta(0,60)]
</code></pre>

<p>It seems to work, do you have any comments?</p>
