<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ETag and Last-Modified Headers — feedparser 5.1.3 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="_static/feedparser.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '5.1.3', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <link rel="top" title="feedparser 5.1.3 documentation" href="index.html" /> <link rel="up" title="HTTP Features" href="http.html" /> <link rel="next" title="User-Agent and Referer Headers" href="http-useragent.html" /> <link rel="prev" title="HTTP Features" href="http.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="http-useragent.html" title="User-Agent and Referer Headers" accesskey="N">next</a> |</li> <li class="right" > <a href="http.html" title="HTTP Features" accesskey="P">previous</a> |</li> <li><a href="index.html">feedparser 5.1.3 documentation</a> »</li> <li><a href="http.html" accesskey="U"><abbr title="Hypertext Transfer Protocol">HTTP</abbr> Features</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="etag-and-last-modified-headers"> <span id="http-etag"></span><h1>ETag and Last-Modified Headers<a class="headerlink" href="#etag-and-last-modified-headers" title="Permalink to this headline">¶</a></h1> <p>ETags and Last-Modified headers are two ways that feed publishers can save bandwidth, but they only work if clients take advantage of them. <strong class="program">Universal Feed Parser</strong> gives you the ability to take advantage of these features, but you must use them properly.</p> <p>The basic concept is that a feed publisher may provide a special <abbr title="Hypertext Transfer Protocol">HTTP</abbr> header, called an ETag, when it publishes a feed. You should send this ETag back to the server on subsequent requests. If the feed has not changed since the last time you requested it, the server will return a special <abbr title="Hypertext Transfer Protocol">HTTP</abbr> status code (<tt class="docutils literal"><span class="pre">304</span></tt>) and no feed data.</p> <div class="section" id="using-etags-to-reduce-bandwidth"> <h2>Using ETags to reduce bandwidth<a class="headerlink" href="#using-etags-to-reduce-bandwidth" title="Permalink to this headline">¶</a></h2> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">feedparser</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">'http://feedparser.org/docs/examples/atom10.xml'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">etag</span> <span class="go">'"6c132-941-ad7e3080"'</span> <span class="gp">>>> </span><span class="n">d2</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">'http://feedparser.org/docs/examples/atom10.xml'</span><span class="p">,</span> <span class="n">etag</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">etag</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d2</span><span class="o">.</span><span class="n">status</span> <span class="go">304</span> <span class="gp">>>> </span><span class="n">d2</span><span class="o">.</span><span class="n">feed</span> <span class="go">{}</span> <span class="gp">>>> </span><span class="n">d2</span><span class="o">.</span><span class="n">entries</span> <span class="go">[]</span> <span class="gp">>>> </span><span class="n">d2</span><span class="o">.</span><span class="n">debug_message</span> <span class="go">'The feed has not changed since you last checked, so</span> <span class="go">the server sent no data. This is a feature, not a bug!'</span> </pre></div> </div> <p>There is a related concept which accomplishes the same thing, but slightly differently. In this case, the server publishes the last-modified date of the feed in the <abbr title="Hypertext Transfer Protocol">HTTP</abbr> header. You can send this back to the server on subsequent requests, and if the feed has not changed, the server will return <abbr title="Hypertext Transfer Protocol">HTTP</abbr> status code <tt class="docutils literal"><span class="pre">304</span></tt> and no feed data.</p> </div> <div class="section" id="using-last-modified-headers-to-reduce-bandwidth"> <h2>Using Last-Modified headers to reduce bandwidth<a class="headerlink" href="#using-last-modified-headers-to-reduce-bandwidth" title="Permalink to this headline">¶</a></h2> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">feedparser</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">'http://feedparser.org/docs/examples/atom10.xml'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">modified</span> <span class="go">Fri, 11 Jun 2012 23:00:34 GMT</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">modified_parsed</span> <span class="go">(2004, 6, 11, 23, 0, 34, 4, 163, 0)</span> <span class="gp">>>> </span><span class="n">d2</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">'http://feedparser.org/docs/examples/atom10.xml'</span><span class="p">,</span> <span class="n">modified</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">modified</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d2</span><span class="o">.</span><span class="n">status</span> <span class="go">304</span> <span class="gp">>>> </span><span class="n">d2</span><span class="o">.</span><span class="n">feed</span> <span class="go">{}</span> <span class="gp">>>> </span><span class="n">d2</span><span class="o">.</span><span class="n">entries</span> <span class="go">[]</span> <span class="gp">>>> </span><span class="n">d2</span><span class="o">.</span><span class="n">debug_message</span> <span class="go">'The feed has not changed since you last checked, so</span> <span class="go">the server sent no data. This is a feature, not a bug!'</span> </pre></div> </div> <p>Clients should support both ETag and Last-Modified headers, as some servers support one but not the other.</p> <div class="admonition important"> <p class="first admonition-title">Important</p> <p class="last">If you do not support ETag and Last-Modified headers, you will repeatedly download feeds that have not changed. This wastes your bandwidth and the publisher’s bandwidth, and the publisher may ban you from accessing their server.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">You can control the behaviour of <abbr title="Hypertext Transfer Protocol">HTTP</abbr> caches between your application and the origin server by using the <tt class="docutils literal"><span class="pre">extra_headers</span></tt> parameter. For example, you may want to send <tt class="docutils literal"><span class="pre">Cache-control:</span> <span class="pre">max-age=60</span></tt> to make the caches revalidate against the origin server unless their cached copy is less than a minute old. Again, this should be used with consideration.</p> </div> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <ul class="last simple"> <li><a class="reference external" href="http://fishbowl.pastiche.org/2002/10/21/http_conditional_get_for_rss_hackers">HTTP Conditional Get For RSS Hackers</a></li> <li><a class="reference external" href="http://diveintopython.org/http_web_services/">HTTP Web Services</a></li> </ul> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">ETag and Last-Modified Headers</a><ul> <li><a class="reference internal" href="#using-etags-to-reduce-bandwidth">Using ETags to reduce bandwidth</a></li> <li><a class="reference internal" href="#using-last-modified-headers-to-reduce-bandwidth">Using Last-Modified headers to reduce bandwidth</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="http.html" title="previous chapter"><abbr title="Hypertext Transfer Protocol">HTTP</abbr> Features</a></p> <h4>Next topic</h4> <p class="topless"><a href="http-useragent.html" title="next chapter">User-Agent and Referer Headers</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/http-etag.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="http-useragent.html" title="User-Agent and Referer Headers" >next</a> |</li> <li class="right" > <a href="http.html" title="HTTP Features" >previous</a> |</li> <li><a href="index.html">feedparser 5.1.3 documentation</a> »</li> <li><a href="http.html" ><abbr title="Hypertext Transfer Protocol">HTTP</abbr> Features</a> »</li> </ul> </div> <div class="footer"> © Copyright 2004-2008 Mark Pilgrim, 2010-2012 Kurt McKee. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>