http-etag.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. .. _http.etag:
  2. ETag and Last-Modified Headers
  3. ==============================
  4. ETags and Last-Modified headers are two ways that feed publishers can save
  5. bandwidth, but they only work if clients take advantage of them.
  6. :program:`Universal Feed Parser` gives you the ability to take advantage of
  7. these features, but you must use them properly.
  8. The basic concept is that a feed publisher may provide a special
  9. :abbr:`HTTP (Hypertext Transfer Protocol)` header, called an ETag, when it
  10. publishes a feed. You should send this ETag back to the server on subsequent
  11. requests. If the feed has not changed since the last time you requested it,
  12. the server will return a special :abbr:`HTTP (Hypertext Transfer Protocol)`
  13. status code (``304``) and no feed data.
  14. Using ETags to reduce bandwidth
  15. -------------------------------
  16. ::
  17. >>> import feedparser
  18. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
  19. >>> d.etag
  20. '"6c132-941-ad7e3080"'
  21. >>> d2 = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml', etag=d.etag)
  22. >>> d2.status
  23. 304
  24. >>> d2.feed
  25. {}
  26. >>> d2.entries
  27. []
  28. >>> d2.debug_message
  29. 'The feed has not changed since you last checked, so
  30. the server sent no data. This is a feature, not a bug!'
  31. There is a related concept which accomplishes the same thing, but slightly
  32. differently. In this case, the server publishes the last-modified date of the
  33. feed in the :abbr:`HTTP (Hypertext Transfer Protocol)` header. You can send
  34. this back to the server on subsequent requests, and if the feed has not
  35. changed, the server will return :abbr:`HTTP (Hypertext Transfer Protocol)`
  36. status code ``304`` and no feed data.
  37. Using Last-Modified headers to reduce bandwidth
  38. -----------------------------------------------
  39. ::
  40. >>> import feedparser
  41. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
  42. >>> d.modified
  43. Fri, 11 Jun 2012 23:00:34 GMT
  44. >>> d.modified_parsed
  45. (2004, 6, 11, 23, 0, 34, 4, 163, 0)
  46. >>> d2 = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml', modified=d.modified)
  47. >>> d2.status
  48. 304
  49. >>> d2.feed
  50. {}
  51. >>> d2.entries
  52. []
  53. >>> d2.debug_message
  54. 'The feed has not changed since you last checked, so
  55. the server sent no data. This is a feature, not a bug!'
  56. Clients should support both ETag and Last-Modified headers, as some servers support one but not the other.
  57. .. important::
  58. If you do not support ETag and Last-Modified headers, you will repeatedly
  59. download feeds that have not changed. This wastes your bandwidth and the
  60. publisher's bandwidth, and the publisher may ban you from accessing their
  61. server.
  62. .. note::
  63. You can control the behaviour of :abbr:`HTTP (Hypertext Transfer Protocol)`
  64. caches between your application and the origin server by using the
  65. ``extra_headers`` parameter. For example, you may want to send
  66. ``Cache-control: max-age=60`` to make the caches revalidate against the
  67. origin server unless their cached copy is less than a minute old. Again,
  68. this should be used with consideration.
  69. .. seealso::
  70. * `HTTP Conditional Get For RSS Hackers <http://fishbowl.pastiche.org/2002/10/21/http_conditional_get_for_rss_hackers>`_
  71. * `HTTP Web Services <http://diveintopython.org/http_web_services/>`_