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

Expanding Your Application - 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

Of course, having just one method is useless and wasteful. Additional methods may certainly be created, and they may be access by appending their names to the address. For example, let's create a file that contains multiple methods and test it out:

import cherrypy

class MultiMethod:

   def index ( self ):
      return "<a href='A'>( Method A )</a>"
   def A ( self ):
      return "<a href='B'>( Method B )</a>"
   def B ( self ):
      return "<a href='C'>( Method C )</a>"
   def C ( self ):
      return "<a href=''>( Index )</a>"
   index.exposed = A.exposed = B.exposed = C.exposed = True

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

As you can see, each method contains a link to the next method. Of course, having a bunch of methods thrown around a file isn't going to help you when it comes to organization. That's why CherryPy also allows for multiple objects in your applications, as seen here:

import cherrypy

class DefaultClass:

   def index ( self ):
      return "<a href='A'>( Object A )</a>"
   index.exposed = True

class A:

   def index ( self ):
      return "<a href='a'>( Method a )</a>"
   def a ( self ):
      return "<a href='/B'>( Object B )</a>"
   index.exposed = a.exposed = True

class B:

   def index ( self ):
      return "<a href='b'>( Method b )</a>"
   def b ( self ):
      return "<a href='/C'>( Object C )</a>"
   index.exposed = b.exposed = True

class C:

   def index ( self ):
      return "<a href='c'>( Method c )</a>"
   def c ( self ):
      return "<a href='/index'>( Index )</a>"
   index.exposed = c.exposed = True

cherrypy.root = DefaultClass()
cherrypy.root.A = A()
cherrypy.root.B = B()
cherrypy.root.C = C()

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

If you run the application, you will see that we have various objects and their methods mapped to different addresses:

http://localhost:8080

http://localhost:8080/A

http://localhost:8080/A/a

http://localhost:8080/B

http://localhost:8080/B/b

http://localhost:8080/C

http://localhost:8080/C/c

Notice that we assign an instance of each class to a variable under root. This is necessary for CherryPy to figure out which objects you want to let users access and where you want each object in the address map. You could also nest objects within the map:

import cherrypy

class DefaultClass:

   def index ( self ):
      return "<a href='A'>( Object A )</a>"
   index.exposed = True

class A:

   def index ( self ):
      return "<a href='B'>( Object B )</a>"
   index.exposed = True

class B:

   def index ( self ):
      return "<a href='C'>( Object C )</a>"
   index.exposed = True

class C:

   def index ( self ):
      return "<a href='/'>( Index )</a>"
   index.exposed = True

cherrypy.root = DefaultClass()
cherrypy.root.A = A()
cherrypy.root.A.B = B()
cherrypy.root.A.B.C = C()

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

Now, C is inside B, which is inside A:

http://localhost:8080

http://localhost:8080/A

http://localhost:8080/A/B

http://localhost:8080/A/B/C



 
 
>>> 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: