Python
  Home arrow Python arrow Page 3 - 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 - Expanding Your Application
    ( Page 3 of 4 )

    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
     

       

    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