httplib2" no-cache"在.py文件中不起作用

时间:2016-01-26 06:20:08

标签: python terminal cache-control httplib2

我在Aptana Studio中创建了以下.py文件来测试HTML请求(使用GET方法)是否到达缓存或实际服务器:

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()

如果我在终端中启动python3,并使用' cache-control'输入请求。设置为' no-cache',它实际访问服务器,服务器的响应打印如下:

>>>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

但是,如果我运行.py脚本;或者从终端启动python3,将.py文件作为模块导入并运行testNoCacheResponse()函数,它既不访问服务器也不打印来自服务器的响应。

请指教!谢谢!

0 个答案:

没有答案