用Python实现应用Last-Modified和ETag避免下载重复内容
2014-12-23 22:55
579 查看
用Python实现应用Last-Modified和ETag避免下载重复内容
Http 1.1中避免重复下载的标记
使用Http1.1中定义好的头信息来避免重复下载,参考HTTP/1.1 Section 14 Header Field Definitions中的14.19ETag/14.24 If-Match/14.29 Last-Modified/14.25 If-Modified-Since
开发者把Last-Modified 和ETags请求的http报头一起使用,能够有效利用本地缓存,降低无谓的重复下载。
示例代码逻辑
1. 客户端下载一个链接(Sample);2. 服务器返回Sample,Sample中记录Last-Modified/ETag标记;
3. 客户端再次下载这个链接,并将上次请求时服务器返回的Last-Modified/ETag一起传递给服务器;
4. 服务器检查该Last-Modified或ETag,并判断出该页面自上次客户端请求之后还未被修改,直接返回响应304和一个空的响应体。
其实在《Dive Into Python》中就有相当详细的实例代码,强烈建议没看过这本书的python程序员们认真学习一下,会提升面向对象编程和网络编程能力的。
示例代码
?Download HttpDownload.txt1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on Nov 9, 2011 @author: li3huo ''' import urllib, urllib2 import sys import time class DefaultErrorHandler(urllib2.HTTPDefaultErrorHandler): """用来保证请求中记录Http状态 """ def http_error_default(self, req, fp, code, msg, headers): result = urllib2.HTTPError( req.get_full_url(), code, msg, headers, fp) result.status = code return result class Sample(): """a sample is the url i want to download """ url = None contentLength = 0 etag = None lastModified = None data = None path = None def __init__(self, url, contentLength=0, etag=None, lastModified=None): self.url = url self.contentLength = 0 self.etag = etag self.lastModified = lastModified self.status = 200 self.file = file def __repr__(self): return repr("Http Status=%d; Length=%d; Last Modified Time=%s; eTag=%s" % (self.status, self.contentLength, self.lastModified, self.etag)) def downloadSample(self): request = urllib2.Request(self.url) request.add_header('User-Agent', "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)") if self.lastModified: request.add_header('If-Modified-Since', self.lastModified) if self.etag: request.add_header('If-None-Match', self.etag) conn = urllib2.build_opener(DefaultErrorHandler()).open(request) if hasattr(conn, 'headers'): # save ETag, if the server sent one self.etag = conn.headers.get('ETag') # save Last-Modified header, if the server sent one self.lastModified = conn.headers.get('Last-Modified') self.contentLength = conn.headers.get("content-length") if hasattr(conn, 'status'): self.status = conn.status print "status=%d" % self.status self.data = conn.read() if self.status == 304: print "the content is same, so return nothing!" if not self.contentLength: self.contentLength = len(self.data) conn.close() if __name__ == '__main__': url = 'http://www.sina.com.cn' sample = Sample(url) sample.downloadSample() print sample sample.downloadSample() print sample |
‘Http Status=200; Length=589988; Last Modified Time=Wed, 09 Nov 2011 10:45:55 GMT; eTag=None’
status=304
the content is same, so return nothing!
‘Http Status=304; Length=0; Last Modified Time=Wed, 09 Nov 2011 10:45:55 GMT; eTag=None’
相关文章推荐
- http响应Last-Modified和ETag以及asp.net web api实现
- http响应Last-Modified和ETag以及asp.net web api实现
- 登陆新浪微博&批量下载收藏内容[Python脚本实现]
- 利用Python脚本实现-----登陆新浪微博&批量下载收藏内容
- http响应Last-Modified和ETag以及asp.net web api实现
- http响应Last-Modified和ETag以及asp.net web api实现(转载)
- Scrapy:Python实现scrapy框架爬虫两个网址下载网页内容信息——Jason niu
- http响应Last-Modified和ETag以及asp.net web api实现
- 登陆新浪微博&批量下载收藏内容[Python脚本实现]
- 浏览器缓存及应用Cache-Control(用于本地缓存),Expires(用于本地缓存),Last-Modified(协商缓存),Etag(协商缓存)
- 深入剖析ExtJS 2.2实现及应用连载(12):主内容区域
- 下载糗事百科的内容_python版
- 用python实现下载百度mp3
- 下载糗事百科内容_python_多进程版
- Web应用中避免Form重复提交的三种方案
- Python实现文件上传下载的SOAP Client
- Web应用中避免Form重复提交的三种方案
- 关于http的Last-Modified和ETag
- 针式PKM初级应用:如何避免收集重复的资料?
- HttpHeader:Last-Modified,ETag,Cache-Control,Expires 设置页面过期策略