README 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. What is this and what is it for?
  2. --------------------------------
  3. cookies.py is a Python module for working with HTTP cookies: parsing and
  4. rendering 'Cookie:' request headers and 'Set-Cookie:' response headers,
  5. and exposing a convenient API for creating and modifying cookies. It can be
  6. used as a replacement of Python's Cookie.py (aka http.cookies).
  7. Features
  8. --------
  9. * Rendering according to the excellent new RFC 6265
  10. (rather than using a unique ad hoc format inconsistently relating to
  11. unrealistic, very old RFCs which everyone ignored). Uses URL encoding to
  12. represent non-ASCII by default, like many other languages' libraries
  13. * Liberal parsing, incorporating many complaints about Cookie.py barfing
  14. on common cookie formats which can be reliably parsed (e.g. search 'cookie'
  15. on the Python issue tracker)
  16. * Well-documented code, with chapter and verse from RFCs
  17. (rather than arbitrary, undocumented decisions and huge tables of magic
  18. values, as you see in Cookie.py).
  19. * Test coverage at 100%, with a much more comprehensive test suite
  20. than Cookie.py
  21. * Single-source compatible with the following Python versions:
  22. 2.6, 2.7, 3.2, 3.3 and PyPy (2.7).
  23. * Cleaner, less surprising API::
  24. # old Cookie.py - this code is all directly from its docstring
  25. >>> from Cookie import SmartCookie
  26. >>> C = SmartCookie()
  27. >>> # n.b. it's "smart" because it automatically pickles Python objects,
  28. >>> # which is actually quite stupid for security reasons!
  29. >>> C["rocky"] = "road"
  30. >>> C["rocky"]["path"] = "/cookie"
  31. >>> # So C["rocky"] is a string, except when it's a dict...
  32. >>> # and why do I have to write [""] to access a fixed set of attrs?
  33. >>> # Look at the atrocious way I render out a request header:
  34. >>> C.output(attrs=[], header="Cookie:")
  35. 'Cookie: rocky=road'
  36. # new cookies.py
  37. >>> from cookies import Cookies, Cookie
  38. >>> cookies = Cookies(rocky='road')
  39. >>> # Can also write explicitly: cookies['rocky'] = Cookie['road']
  40. >>> cookies['rocky'].path = "/cookie"
  41. >>> cookies.render_request()
  42. 'rocky=road'
  43. * Friendly to customization, extension, and reuse of its parts.
  44. Unlike Cookie.py, it doesn't lock all implementation inside its own classes
  45. (forcing you to write ugly wrappers as Django, Trac, Werkzeug/Flask, web.py
  46. and Tornado had to do). You can suppress minor parse exceptions with
  47. parameters rather than subclass wrappers. You can plug in your own parsers,
  48. renderers and validators for new or existing cookie attributes. You can
  49. render the data out in a dict. You can easily use the underlying imperative
  50. API or even lift the parser's regexps for your own parser or project. They
  51. are very well documented and relate directly to RFCs, so you know exactly
  52. what you are getting and why. It's MIT-licensed so do
  53. what you want (but I'd love to know what use you are getting from it!)
  54. * One file, so you can just drop cookies.py into your project if you like
  55. * MIT license, so you can use it in whatever you want with no strings
  56. Things this is not meant to do
  57. ------------------------------
  58. While this is intended to be a good module for handling cookies, it does not
  59. even try to do any of the following:
  60. * Maintain backward compatibility with Cookie.py, which would mean
  61. inheriting its confusions and bugs
  62. * Implement RFCs 2109 or 2965, which have always been ignored by almost
  63. everyone and are now obsolete as well
  64. * Handle every conceivable output from terrible legacy apps, which is not
  65. possible to do without lots of silent data loss and corruption (the
  66. parser does try to be liberal as possible otherwise, though)
  67. * Provide a means to store pickled Python objects in cookie values
  68. (that's a big security hole)
  69. This doesn't compete with the cookielib (http.cookiejar) module in the Python
  70. standard library, which is specifically for implementing cookie storage and
  71. similar behavior in an HTTP client such as a browser. Things cookielib does
  72. that this doesn't:
  73. * Write to or read from browsers' cookie stores or other proprietary
  74. formats for storing cookie data in files
  75. * Handle the browser/client logic like deciding which cookies to send or
  76. discard, etc.
  77. If you are looking for a cookie library but neither this one nor cookielib
  78. will help, you might also consider the implementations in WebOb or Bottle.