http-redirect.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. :abbr:`HTTP (Hypertext Transfer Protocol)` Redirects
  2. ====================================================
  3. When you download a feed from a remote web server, :program:`Universal Feed Parser`
  4. exposes the :abbr:`HTTP (Hypertext Transfer Protocol)` status code. You need
  5. to understand the different codes, including permanent and temporary redirects,
  6. and feeds that have been marked "gone".
  7. When a feed has temporarily moved to a new location, the web server will return
  8. a ``302`` status code. :program:`Universal Feed Parser` makes this available
  9. in ``d.status``.
  10. There is nothing special you need to do with temporary redirects; by the time
  11. you learn about it, :program:`Universal Feed Parser` has already followed the
  12. redirect to the new location (available in ``d.href``), downloaded the feed,
  13. and parsed it. Since the redirect is temporary, you should continue requesting
  14. the original :abbr:`URL (Uniform Resource Locator)` the next time you want to
  15. parse the feed.
  16. Noticing temporary redirects
  17. ----------------------------
  18. ::
  19. >>> import feedparser
  20. >>> d = feedparser.parse('http://feedparser.org/docs/examples/temporary.xml')
  21. >>> d.status
  22. 302
  23. >>> d.href
  24. 'http://feedparser.org/docs/examples/atom10.xml'
  25. >>> d.feed.title
  26. u'Sample Feed'
  27. When a feed has permanently moved to a new location, the web server will return
  28. a ``301`` status code. Again, :program:`Universal Feed Parser` makes this
  29. available in ``d.status``.
  30. If you are polling a feed on a regular basis, it is very important to check the
  31. status code (``d.status``) every time you download. If the feed has been
  32. permanently redirected, you should update your database or configuration file
  33. with the new address (``d.href``). Repeatedly requesting the original address
  34. of a feed that has been permanently redirected is very rude, and may get you
  35. banned from the server.
  36. Noticing permanent redirects
  37. ----------------------------
  38. ::
  39. >>> import feedparser
  40. >>> d = feedparser.parse('http://feedparser.org/docs/examples/permanent.xml')
  41. >>> d.status
  42. 301
  43. >>> d.href
  44. 'http://feedparser.org/docs/examples/atom10.xml'
  45. >>> d.feed.title
  46. u'Sample Feed'
  47. When a feed has been permanently deleted, the web server will return a ``410``
  48. status code. If you ever receive a ``410``, you should stop polling the feed
  49. and inform the end user that the feed is gone for good.
  50. Repeatedly requesting a feed that has been marked as "gone" is very rude, and
  51. may get you banned from the server.
  52. Noticing feeds marked "gone"
  53. ----------------------------
  54. ::
  55. >>> import feedparser
  56. >>> d = feedparser.parse('http://feedparser.org/docs/examples/gone.xml')
  57. >>> d.status
  58. 410