BaseHTTPRequestHandler - bypassing browser cache

B

Ben

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
 
J

JanC

(e-mail address removed) (Ben) schreef:
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??

Did you read the HTTP/1.1 specification?

RFC-6216, section 13 "Caching in HTTP":
<http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top