BaseHTTPRequestHandler - bypassing browser cache

Ben crescent_au at yahoo.com
Fri Oct 17 23:49:12 EDT 2003


Hi all,

I have written a web server that listens for requests:

class MyHandler(BaseHTTPRequestHandler):

  def do_GET(self):
    if (self.path)=='/':
      data=makeList()
    else:
      q=re.findall('\/(.*)',self.path)
      data=makeList()
      data += makeDetail(q[0])
    self.send_response(200)
    #self.send_header('Content-Length', str(len(data)))
    self.send_header('Content-type', 'text/html')
    self.end_headers()
    self.wfile.write(data)

httpd=HTTPServer(('',9857), MyHandler)
httpd.serve_forever()

My browser displays links in the following way:

     Peter
     -----
     Jack
     ----
     Jane
     ----

I also have a cache file named "cache.ch" that stores the above data.
So when I make a request from my browser "http://localhost:9857/", it
first goes and checks the cache file for data, if it's not there, it
fetches the data from the web and also appends that to the cache file.
This is done by the "makeList()" method:

def makeList():
  ppl=[]
  p=''
  if os.path.exists("cache.ch"):
    cachefile=open_file("cache.ch")
    ppl=cachefile[0].split(' ')
    for c in ppl:
      p += '<a href=http://localhost:9857/%s>'%c+c+'</a><br>'
  else:
    r=read_url_from_web("http://www.xxxx.xxx")
    z=r.read()
    m=getList(z)
    for k in m:
      write_to_file("cache.ch",k+' ',access_mode='a')
      p += '<a href=http://localhost:9777/%s>'%k+k+'</a><br>'
  return p

Similarly, I have "makeDetail()" method that fetches data from the web
or cache file when I click on say "Peter".

My problem is whenever I access a link from the browser, even the
starting page "http://localhost:9857/", it doesn't seem to read the
cache file. It just seems to return me the data from browser cache,
unless I press the reload button on the browser, then it fetches it
from the internet and updates the cache file.

What seems to be the problem?? I'd like to always read from my cache
file first. Any suggestions??

Thanks
Ben




More information about the Python-list mailing list