opensourcejason.info
py simple web

Server

import BaseHTTPServer
import os
#Request handler can handle a special header item called
#Field-Time: <current client time>

class AnytimeHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_GET(self):
        basedir = "../fetch/group1/"
        path = self.path
        path = path.lstrip("/");
        clienttime = self.headers['client-time']
        ims = self.headers['if-modified-since']

        name="%s-%s"%(clienttime,path)
        mod_name="%s-%s"%(ims,path)
        print "Looking for",name
        print "Later than",mod_name
        #Read all filenames ending with path
        feeds=os.listdir(basedir)
        filtered=[]
        print len(feeds)
        i=0

        for item in feeds:
            i+=1
            if item[-len(path):]==path and item <= name:
                filtered.append(item)

        try:
            latest=max(filtered)
            print len(filtered)
            if latest<mod_name:
                self.send_response(304, "Not Modified");
            else:
                self.send_response(200)
                self.send_header('Content-type',        'text/xml')
                self.end_headers()
                f=open(basedir+latest,'r')
                print "Returning",basedir+latest
                self.wfile.write(f.read())
        except:
            self.send_response(100, "Unfound")
            latest=None
if __name__=="__main__":

    httpd = BaseHTTPServer.HTTPServer(('',8000),AnytimeHTTPRequestHandler)
    httpd.serve_forever()

Client

import urllib2
import datetime

#Test client behavior:
#1. Start time at the 2nd feed fetch.
#2. Fetch feed.
#3. Increment timer
#4. Fetch next feed using IMS=last feed time, and client-time=timer
class mydatetime(datetime.datetime):
    def __str__(self):
        return "%.2d-%.2d-%.2d_%.2d:%.2d:%.2d"%(self.year,self.month,self.day,self.hour,self.minute,self.second)
    def cast(self,dt):
        return self.replace(year=dt.year, month=dt.month, day=dt.day, hour=dt.hour, minute=dt.minute, second=dt.second)


timer=mydatetime(2006,12,31,14,30,00)
end_time=mydatetime(2007,1,2,10,30,0)
last_time=""

while timer < end_time:
    req=urllib2.Request("http://localhost:8000/Slashdot",headers={"If-Modified-Since":last_time,"Client-Time":timer})
    try:
        response=urllib2.urlopen(req)
        print "Got new"
        last_time=timer
    except urllib2.HTTPError, e:
        print "Error: ",e,last_time, timer

    dtimer=timer+datetime.timedelta(0,0,0,0,10)
    timer=timer.cast(dtimer)

Add Comment 
Sign as Author 
Enter code 526