Python
  Home arrow Python arrow Page 4 - CherryPy: Object-Oriented Web Development
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

CherryPy: Object-Oriented Web Development
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-01-10


    Table of Contents:
  • CherryPy: Object-Oriented Web Development
  • A Simple Script
  • Expanding Your Application
  • Handling User Input

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    CherryPy: Object-Oriented Web Development - Handling User Input
    ( Page 4 of 4 )

    Your application is probably going to have to deal with user input somewhere along the line, and, fortunately, CherryPy makes user input easy to work with. User input is passed in the form of arguments to methods, like this:

    import cherrypy

    class InputExample:

       def index ( self ):
          return "<form method='POST' action='submit'>\n" + \
                 "E-Mail: <input type='text' name='email' />\n" + \
                 "<br /><input type='submit' value='Submit' />\n" + \
                 "</form>"
       def submit ( self, email = None ):
          if email:
             return "Thank you for your submission, " + email + "."
          else:
             return "You have not submitted anything!"
       index.exposed = submit.exposed = True

    cherrypy.root = InputExample()
    cherrypy.config.update ( file = 'development.conf' )
    cherrypy.server.start()

    In the above example, we set the default value of email to None, but, of course, you can set it to whatever value you would like. As you can see above, handling user input is not a complex issue. Simply make your function accept user input as arguments, and then manipulate or access those variables as needed.

    User Sessions

    Sessions are used to store user data in between pages. In order to access them in CherryPy, we must modify our configuration file a bit to enable sessions:

    [global]

    server.socketPort = 8080
    server.environment = "development"
    server.threadPool = 10
    sessionFilter.on = True

    Now, we are able to access sessions via the cherrypy.session object. Here's a simple session example:

    import cherrypy

    class SessionExample:

       def index ( self ):
          if cherrypy.session.has_key ( 'color' ):
             out =  "<font color='%s'>%s</font>" % ( cherrypy.session
    [ 'color' ], cherrypy.session [ 'color' ] )
          else:
             out = ""
          return out + "<form method='POST' action='setColor'>\n" + \
                 "Please choose a color:<br />\n" + \
                 "<select name='color'>\n" + \
                 "<option>Black</option>\n" + \
                 "<option>Red</option>\n" + \
                 "<option>Green</option>\n" + \
                 "<option>Blue</option>\n" + \
                 "</select><br />\n" + \
                 "<input type='submit' value='Select' />\n" + \
                 "</form>"
       def setColor ( self, color ):
          cherrypy.session [ 'color' ] = color
          return "<a href=''>Back</a>"
       index.exposed = setColor.exposed = True

    cherrypy.config.update ( file = 'development.conf' )
    cherrypy.root = SessionExample()
    cherrypy.server.start()

    When the user first visits the page, he or she sees a form with a color field. When the user selects a value, that value is stored inside of a session, and a link back to the first page is displayed. This time, however, the user will see the name of the chosen color displayed in the chosen color. The form will also be visible so that the chosen color may be changed.

    Sessions work fairly simply. Each session name is stored as a key, with the session's value stored as the value of that key. We can check for the existence of a particular key by using the has_key method of cherrypy.session. Again, however, sessionFilter.on must be set to True in order for sessions to be enabled in your CherryPy application.

    Conclusion

    Though there are quite a few different web development platforms available for Python, CherryPy stands out because it makes heavy use of object orientation. Each page is a method contained in an object, and the elements in a request match the names of objects and methods. Moreover, each CherryPy script is actually an independent server. Tasks such as accepting user input and manipulating user sessions are especially easy with CherryPy, and making CherryPy work behind Apache is an easy feat. CherryPy allows for simple, object oriented web development in a simple, object oriented language.



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek