Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CherryPy

 CherryPy

Avatar for Seth House

Seth House

May 09, 2013
Tweet

More Decks by Seth House

Other Decks in Technology

Transcript

  1. A minimalist Python web framework Outline 1 A minimalist Python

    web framework 2 CherryPy basics 3 Building an app 4 Conclusion Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 2 / 28
  2. A minimalist Python web framework History Remi Delon 2002: CherryPy

    CherryPy class processed to be self-contained module (app & server) Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 4 / 28
  3. A minimalist Python web framework History Remi Delon 2002: CherryPy

    CherryPy class processed to be self-contained module (app & server) 2004: CherryPy 2 Object publishing Filters Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 4 / 28
  4. A minimalist Python web framework History Remi Delon 2002: CherryPy

    CherryPy class processed to be self-contained module (app & server) 2004: CherryPy 2 Object publishing Filters 2005: CherryPy 2.1 Shipped with Turbogears Scrutiny; performance; WSGI support Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 4 / 28
  5. A minimalist Python web framework History Remi Delon 2002: CherryPy

    CherryPy class processed to be self-contained module (app & server) 2004: CherryPy 2 Object publishing Filters 2005: CherryPy 2.1 Shipped with Turbogears Scrutiny; performance; WSGI support 2006: CherryPy 3 Book Turbogears 2.x chose Pylons Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 4 / 28
  6. A minimalist Python web framework History Remi Delon 2002: CherryPy

    CherryPy class processed to be self-contained module (app & server) 2004: CherryPy 2 Object publishing Filters 2005: CherryPy 2.1 Shipped with Turbogears Scrutiny; performance; WSGI support 2006: CherryPy 3 Book Turbogears 2.x chose Pylons 2011: CherryPy 3.2 Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 4 / 28
  7. A minimalist Python web framework Why CherryPy 2005 2006 2007

    2008 2009 2010 2011 2012 2013 0 100 200 Figure: https://www.ohloh.net/p/cherrypy Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 5 / 28
  8. A minimalist Python web framework Goals Simplicity No deps Lightly

    opinionated Community driven Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 6 / 28
  9. A minimalist Python web framework Features Small (~600k) Featureful Plain

    functions or objects Extremely extendible Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 7 / 28
  10. CherryPy basics Outline 1 A minimalist Python web framework 2

    CherryPy basics 3 Building an app 4 Conclusion Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 8 / 28
  11. CherryPy basics Framework Configuration via dictionaries Seven hook functions Called

    during request/response cycle Caching Encoding Sessions & cookies Authorization Uploads Static content Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 11 / 28
  12. CherryPy basics Framework Configuration via dictionaries Seven hook functions Called

    during request/response cycle Caching Encoding Sessions & cookies Authorization Uploads Static content No ORM / no templating / no forms Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 11 / 28
  13. CherryPy basics Server Pure Python (Python 2.3+) HTTP/1.1 compliant Thread

    pooled Fast (1-2 ms per request) SSL (!) Cheroot: Stand-alone version Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 12 / 28
  14. CherryPy basics Hello world Application and server: hello.py import cherrypy

    class HelloWorld: def index(self): return "Hello world!" index.exposed = True cherrypy.quickstart(HelloWorld()) Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 13 / 28
  15. Building an app Outline 1 A minimalist Python web framework

    2 CherryPy basics 3 Building an app 4 Conclusion Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 15 / 28
  16. Building an app Dispatchers Determine handler (via app config and

    URI) Set cherrypy.request.handler Wraps handler Collect config in cherrypy.request.config Extremely open-ended Use a custom by setting request.dispatch http://docs.cherrypy.org/stable/refman/ _cpdispatch.html Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 17 / 28
  17. Building an app Default dispatcher For example: http://localhost/ -> root.index

    http://localhost/onepage -> root.onepage http://localhost/some/page -> root.some.page Or possibly: http://localhost/blog/2005/01/17 -> root.blog(self, year, month, day) Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 18 / 28
  18. Building an app Handlers Any callable Current handler is cherrypy.request.handler

    Replace on the fly Must have exposed=True attribute Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 19 / 28
  19. Building an app Handlers Any callable Current handler is cherrypy.request.handler

    Replace on the fly Must have exposed=True attribute .index attribute will take precedence default callable as fallback Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 19 / 28
  20. Building an app Handlers Any callable Current handler is cherrypy.request.handler

    Replace on the fly Must have exposed=True attribute .index attribute will take precedence default callable as fallback POST data available as kwargs: class MyHandler(object): def search(self, q, lang, page): # do something with ‘‘q‘‘ Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 19 / 28
  21. Building an app Config Dictionaries (!) Python code (run-time) ConfigParser

    ini files Python code (execution-time) Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 20 / 28
  22. Building an app Config Dictionaries (!) Python code (run-time) ConfigParser

    ini files Python code (execution-time) Configure Dispatcher (per URL) request / response object attributes Hooks Tools Logging Server options Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 20 / 28
  23. Building an app Config Dictionaries (!) Python code (run-time) ConfigParser

    ini files Python code (execution-time) Configure Dispatcher (per URL) request / response object attributes Hooks Tools Logging Server options Global config Application config Handler config: class MyHandler(object): _cp_config = {} Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 20 / 28
  24. Building an app Tools Behavior outside handlers Many builtin Register

    / enable in the config: [/images] tools.staticdir.on: True Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 21 / 28
  25. Building an app Tools Behavior outside handlers Many builtin Register

    / enable in the config: [/images] tools.staticdir.on: True Some usable as handler Directly callable Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 21 / 28
  26. Building an app Tools Behavior outside handlers Many builtin Register

    / enable in the config: [/images] tools.staticdir.on: True Some usable as handler Directly callable Usable as decorators: @tools.staticdir(dir=’static’) def images(): ... Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 21 / 28
  27. Building an app Writing tools def mytool(): # something! cherrypy.tools.mytool

    = Tool(’on_some_hook’, mytool) Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 22 / 28
  28. Conclusion Outline 1 A minimalist Python web framework 2 CherryPy

    basics 3 Building an app 4 Conclusion Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 24 / 28
  29. Conclusion Good Crazy flexible Lots of documentation Small Fast No

    deps Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 26 / 28
  30. Conclusion Bad Documentation is scattered / some holes CherryPy 2

    vs. 3 vs. 3.2 Small community Flexibility style can be tough to organize Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 27 / 28
  31. Conclusion Walkthrough of a real app Demo of REST interface

    to Salt Request Response Reading headers Writing headers Redirects Exceptions WSGI Server Writing tests Seth House <[email protected]> (Utah Python) CherryPy 2013-05-09 28 / 28