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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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


       · [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 4 hosted by Hostway