Home arrow Python arrow Page 2 - CherryPy: Object-Oriented Web Development

A Simple Script - Python

Python users who engage in web development may appreciate some CherryPy. It is an object-oriented web development framework that allows you to develop web applications easily. It can run on its own server, or behind Apache. Keep reading to learn more.

TABLE OF CONTENTS:
  1. CherryPy: Object-Oriented Web Development
  2. A Simple Script
  3. Expanding Your Application
  4. Handling User Input
By: Peyton McCullough
Rating: starstarstarstarstar / 14
January 10, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: