1

I created the following .py file in Aptana Studio to test whether an HTML request (using the GET method) reaches the cache or the actual server:

import httplib2
httplib2.debuglevel = 1

def testNoCacheResponse():
    h = httplib2.Http('.cache')

    #This line works as expected - it visits the cached file.
    response, content = h.request("http://www.giantrodents.com/test/python/dive_into_python/chapter_15/gen5.xml")

    #This line does not work as expected - 
    #if run in a .py file or a module, it should visit the server but instead it still visits the cached file.
    #if run as an individual line in python3 launched in terminal, then it works.
    response2, content2 = h.request("http://www.giantrodents.com/test/python/dive_into_python/chapter_15/gen5.xml",\
                                     headers = {'cache‐control' : 'no‐cache'})

testNoCacheResponse()

If I launch python3 in terminal, and enter the request with 'cache-control' set as 'no-cache', it actually visits the server and responses from the server are printed as below:

>>>response2, content2 = h.request("http://www.giantrodents.com/test/python/dive_into_python/chapter_15/gen5.xml",headers = {'cache‐control' : 'no‐cache'})
send: b'GET /test/python/dive_into_python/chapter_15/gen5.xml HTTP/1.1\r\nHost: www.giantrodents.com\r\naccept-encoding: gzip, deflate\r\nuser-agent: Python-httplib2/0.9.2 (gzip)\r\ncache-control: no-cache\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date header: Server header: Last-Modified header: ETag header: Accept-Ranges header: Content-Length header: Cache-Control header: Expires header: Content-Type

However, if I run the .py script; or launch python3 from terminal, import the .py file as a module and run the testNoCacheResponse() function, it does neither visit the server nor print the responses from the server.

Please advise! Thank you!

3
  • 1
    i don't see print() in function - so why should it print something ?
    – furas
    Jan 26, 2016 at 6:33
  • The interactive interpreter prints the result of each line. When run as a script python does not do that. Use print() to see the output.
    – Klaus D.
    Jan 26, 2016 at 6:35
  • 1
    I don't think print() is the problem here because what I was trying to test is whether headers = {'cache‐control' : 'no‐cache'} actually forces the request to bypass the cache. And as a result, it didn't if I run the .py as a script. Since I set httplib2.debuglevel as 1, it SHOULD print out the reply from server automatically - It DID in terminal, and also for the .py file when the requested file was not cached, but once the file is cached, the .py file just can't bypass the cache. But thanks for the help!
    – Capybara
    Jan 26, 2016 at 15:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.