Python
  Home arrow Python arrow Page 4 - CherryPy: Object-Oriented Web Developm...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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? 
PYTHON

CherryPy: Object-Oriented Web Development
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · [i]Python users who engage in web development may appreciate some CherryPy. It is an...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway