Python
  Home arrow Python arrow Page 2 - 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 - 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
     

       

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek