SunQuest
 
       Python
  Home arrow Python arrow Page 2 - 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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    CherryPy: Object-Oriented Web Development - A Simple Script


    (Page 2 of 4 )

    Before we start on a CherryPy application, we need to create a configuration file. This file governs the behavior of CherryPy. Here is a configuration file suitable for development work:

    [global]

    server.socketPort = 8080
    server.environment = "development"
    server.threadPool = 10

    Name it development.conf. Most of it is pretty simple to understand. Under the “global” section, the server.socketPort variable controls which port your application will use. The server.environment variable can be set to either “development” or “production.” The former value will display tracebacks when an error is encountered, along with some very basic statistics at the bottom of each page. The latter value saves tracebacks, but it does not output them to the user's browser. Obviously, it doesn't append any statistics, either. We'll use the “development” option to mess around with, but when you expose your applications to the real world, you should always use the “production” option. The last variable we define, server.threadPool, simply tells CherryPy how many threads to spawn.

    Now, we're ready to create a script. To do this, we simply create a class, and we set an instance of that class as CherryPy's root object. We then load the configuration file and start up CherryPy. Create a file called first.py to do this:

    import cherrypy

    class FirstApplication:

       def index ( self ):
          return "Hey."
       index.exposed = True

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

    Now, run the script and point your browser to it:

    http://localhost:8080

    The index method is called. Notice that we set exposed to True. This makes the method publicly accessible. You will also notice some basic information at the bottom of this page. It looks quite ugly, but once “development” is changed to “production,” the information will go away. To check the traceback generated by the “development” option, simply throw an error:

    import cherrypy

    class FirstApplication:

       def index ( self ):
          raise TypeError
       index.exposed = True

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

    More Python Articles
    More By Peyton McCullough


       · [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