A First CherryPy Application - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

A First CherryPy Application

Description:

def doLogin(self, username=None, password=None): return username ' ' password ... def default(self, year, month, day): return year '-' month '-' day ... – PowerPoint PPT presentation

Number of Views:343
Avg rating:3.0/5.0
Slides: 16
Provided by: este61
Category:

less

Transcript and Presenter's Notes

Title: A First CherryPy Application


1
A First CherryPy Application
  • import cherrypy
  • class HelloWorld
  • def index(self)
  • return "Hello world!"
  • index.exposed True
  • cherrypy.quickstart(HelloWorld())
  • Start the application at the command prompt
    (after navigating to its folder)
  • python hello.py
  • Direct your web browser to http//localhost8080
  • The rendering
  • Hello world!
  • Ctrl-c in the command window to terminate the
    application

2
  • Statement import cherrypy imports the main
    CherryPy module.
  • An instance of class HelloWorld is the object
    that will be published
  • Method index() is called when the root URL for
    the site (e.g., http//localhost8080) is
    requested
  • This method returns the contents of the Web page
    (the 'Hello World!' string)
  • Statement index.exposed True tells CherryPy
    that method index() will be exposed
  • Only exposed methods can be called to answer a
    request
  • Lets the user to select which methods of an
    object are Web accessible
  • Can also place the decoration _at_cherrypy.expose
    immediately before the method
  • _at_cherrypy.expose
  • def index(self)
  • return "Hello world!"

3
  • Statement
  • cherrypy.quickstart(HelloWorld())
  • publishes an instance of the HelloWorld class
  • And it starts the embedded webserver
  • Runs until explicitly interrupted (Ctrl-C)
  • When the application is executed, the CherryPy
    server is started with the default configuration
  • Listening on localhost at port 8080
  • Defaults overridden by using a configuration file
    or dictionary (later)
  • Web server receives the request for URL
    http//localhost8080
  • Searches for the best method to handle the
    request, starting from the HelloWorld instance
  • CherryPy calls HelloWorld().index()
  • Result of the call is sent back to the browser as
    the contents of the index page for the website

4
Publishing Objects vs. Exposing Objects
  • Any object that is attached to the root object is
    published
  • I.e., accessible via the internal URL-to-object
    mapping routine
  • But, to be directly accessible via the Web, the
    object must be exposed
  • CherryPy maps URL requests to objects and calls
    the suitable method automatically
  • The methods that can be called as a result of
    external requests are exposed
  • Theyre exposed by setting the exposed attribute
  • Only exposed objects can be accessed as a result
    of a external query
  • But there are some situations where requests can
    be remapped to any published object, including
    non-exposed ones

5
Finding the Correct Object
  • A web application server not only serves the
    content from static files.
  • Can also map the URL it receives into some object
    and call it
  • The key to understanding how to write a web
    application is to understand how this mapping
    occurs
  • The root of the site is the Application.root
    object
  • When it receives a URL, it breaks it into its
    path components
  • For each path component, tries to find an object
    with same name
  • Starts from root, goes down for each component it
    finds, until it can't find a match or is done

6
  • Consider these examples (root is conceptual,
    referring to the root of the document tree)
  • root HelloWorld()
  • root.onepage OnePage()
  • root.otherpage OtherPage()
  • URL http//localhost/onepage points at the 1st
    object
  • URL http//localhost/otherpage points at the 2nd
  • Consider
  • root.some Page()
  • root.some.page Page()
  • URL http//localhost/some/page is mapped to the
    root.some.page object
  • If this object is exposed (or its index method
    is), its called for that URL

7
  • In our HelloWorld example, adding the
    http//.../onepage to OnePage() mapping could be
    done as
  • class OnePage()
  • def index(self)
  • return "one page!"
  • index.exposed True
  • class HelloWorld(object)
  • onepage OnePage()
  • def index(self)
  • return "hello world"
  • index.exposed True
  • cherrypy.quickstart(HelloWorld())
  • In the address bar of the browser, put
  • http//localhost8080/onepage/

8
The index Method
  • Method index(), like the index.html file, is the
    default page for any internal node in the object
    tree
  • Can take additional keyword arguments, mapped to
    the form variables as sent via its GET or POST
    methods
  • Its only called for a full match on the URL

9
Calling Other Methods
  • CherryPy can also directly call methods in the
    published objects if it receives a URL that is
    directly mapped to theme.g.,
  • class HelloWorld
  • def index(self)
  • return "Hello world!"
  • index.exposed True
  • def foo(self)
  • return 'Foo'
  • foo.exposed True
  • cherrypy.quickstart(HelloWorld())
  • Then request
  • http//localhost8080/foo

10
  • When CherryPy receives a request for the /foo
    URL, it calls the foo() function
  • It can be a plain function, or a method of any
    objectany callable will do
  • If CherryPy finds a full match,
  • and the last object in the match is a callable
  • a method, function, or any other Python object
    that supports the __call__ method
  • and the callable doesnt contain a valid index()
    method,
  • then the object itself is called
  • These rules are needed because classes in Python
    are callables (for producing instances)

11
Receiving Data from HTML Forms
  • Any method called by CherryPycan can receive
    additional data from HTML forms using keyword
    arguments
  • The default value can provide
  • a suitable default value for optional arguments
    or
  • means for the application to detect if some
    values were missing from the request
  • CherryPy supports both the GET and POST method
    for forms
  • Arguments are passed the same way

12
  • import cherrypy
  • class Root
  • def index(self)
  • return """lthtmlgt
  • ltheadgt
  • lttitlegtAn Examplelt/titlegt
  • lt/headgt
  • ltbodygt
  • ltform action"doLogin" method"post"gt
  • ltpgtUsernamelt/pgt
  • ltinput type"text" name"username" value""
  • size"15" maxlength"40"/gt
  • ltpgtPasswordlt/pgt
  • ltinput type"password" name"password"
    value""
  • size"10" maxlength"40"/gt
  • ltpgtltinput type"submit" value"Login"/gtlt/pgt
  • ltpgtltinput type"reset" value"Clear"/gtlt/pgt
  • lt/formgt
  • lt/bodygt

Continued
13
  • def doLogin(self, usernameNone,
    passwordNone)
  • return username ' ' password
  • doLogin.exposed True
  • cherrypy.quickstart(Root())
  • Then request
  • http//localhost8080/

14
Partial Matches and the default Method
  • Partial matches happen when a URL contains
    components that dont map to the object tree
  • Then CherryPy calls method default()
  • Recommended for 2 applications
  • Error handling, e.g., when the user types the
    wrong URL
  • Support for positional arguments
  • E.g., assume you have an application that takes
    the year, month and day as part of the URL
  • http//localhost/blog/2005/01/17
  • The following will map the URL as a call to
  • root.blog.default('2005', '1', '17')

15
  • import cherrypy
  • class Blog()
  • def index(self)
  • return "A blog"
  • index.exposed True
  • def default(self, year, month, day)
  • return year "-" month "-" day
  • default.exposed True
  • class HelloWorld
  • blog Blog()
  • def index(self)
  • return "Hello world!"
  • index.exposed True
  • cherrypy.quickstart(HelloWorld())
  • Theres a partial match only up to the blog
    component
  • default() is called, and the positional
    parameters receive the remaining path components
    as arguments
  • The values are passed as strings
Write a Comment
User Comments (0)
About PowerShow.com