gyorgy@0: """ gyorgy@0: Tutorial - Multiple objects gyorgy@0: gyorgy@0: This tutorial shows you how to create a site structure through multiple gyorgy@0: possibly nested request handler objects. gyorgy@0: """ gyorgy@0: gyorgy@0: import cherrypy, os gyorgy@0: from cherrypy.lib.static import serve_file gyorgy@0: gyorgy@0: PATH = os.path.abspath(os.path.dirname(__file__)) gyorgy@0: class Root(object): pass gyorgy@0: cherrypy.tree.mount(Root(), '/mp3/beat.mp3', config={ gyorgy@0: '/': { gyorgy@0: 'tools.staticdir.on': True, gyorgy@0: 'tools.staticdir.dir': PATH, gyorgy@0: 'tools.staticdir.index': 'mp3/beat.mp3', gyorgy@0: }, gyorgy@0: }) gyorgy@0: gyorgy@0: gyorgy@0: gyorgy@0: class HomePage: gyorgy@0: gyorgy@0: gyorgy@0: def index(self): gyorgy@0: return ''' gyorgy@0:

Hi, this is the home page! Check out the other gyorgy@0: fun stuff on this site:

gyorgy@0: gyorgy@0: ''' gyorgy@0: index.exposed = True gyorgy@0: gyorgy@0: gyorgy@0: class MP3Page: gyorgy@0: def index(self): gyorgy@0: filepath = '/Users/tmc/Documents/LiveMesh/cherry/beat.mp3' gyorgy@0: return serve_file(filepath, "application/x-download", "attachment") gyorgy@0: index.exposed = True gyorgy@0: gyorgy@0: gyorgy@0: gyorgy@0: class AudioPage: gyorgy@0: def index(self): gyorgy@0: #mp3file = 'http://www.voanews.com/MediaAssets2/learningenglish/2011_08/se-exp-death-valley-fixed-03aug11.mp3' gyorgy@0: #mp3file = "/Users/tmc/Documents/LiveMesh/cherry/mp3/mp3_1.html" gyorgy@0: #return open(mp3file) gyorgy@0: mp3file = 'http://localhost:8080/mp3/beat.mp3' gyorgy@0: return ''' gyorgy@0: ''' %mp3file gyorgy@0: index.exposed = True gyorgy@0: gyorgy@0: class JokePage: gyorgy@0: def index(self): gyorgy@0: return ''' gyorgy@0:

"In Python, how do you create a string of random gyorgy@0: characters?" -- "Read a Perl file!"

gyorgy@0:

[Return]

''' gyorgy@0: index.exposed = True gyorgy@0: gyorgy@0: gyorgy@0: class LinksPage: gyorgy@0: def __init__(self): gyorgy@0: # Request handler objects can create their own nested request gyorgy@0: # handler objects. Simply create them inside their __init__ gyorgy@0: # methods! gyorgy@0: self.extra = ExtraLinksPage() gyorgy@0: gyorgy@0: def index(self): gyorgy@0: # Note the way we link to the extra links page (and back). gyorgy@0: # As you can see, this object doesn't really care about its gyorgy@0: # absolute position in the site tree, since we use relative gyorgy@0: # links exclusively. gyorgy@0: return ''' gyorgy@0:

Here are some useful links:

gyorgy@0: gyorgy@0: gyorgy@0: gyorgy@0:

You can check out some extra useful gyorgy@0: links here.

gyorgy@0: gyorgy@0:

[Return]

gyorgy@0: ''' gyorgy@0: index.exposed = True gyorgy@0: gyorgy@0: gyorgy@0: class ExtraLinksPage: gyorgy@0: def index(self): gyorgy@0: # Note the relative link back to the Links page! gyorgy@0: return ''' gyorgy@0:

Here are some extra useful links:

gyorgy@0: gyorgy@0: gyorgy@0: gyorgy@0:

[Return to links page]

''' gyorgy@0: index.exposed = True gyorgy@0: gyorgy@0: gyorgy@0: # Of course we can also mount request handler objects right here! gyorgy@0: root = HomePage() gyorgy@0: root.joke = JokePage() gyorgy@0: root.links = LinksPage() gyorgy@0: root.audio = AudioPage() gyorgy@0: root.mp3 = MP3Page() gyorgy@0: gyorgy@0: # Remember, we don't need to mount ExtraLinksPage here, because gyorgy@0: # LinksPage does that itself on initialization. In fact, there is gyorgy@0: # no reason why you shouldn't let your root object take care of gyorgy@0: # creating all contained request handler objects. gyorgy@0: gyorgy@0: gyorgy@0: import os.path gyorgy@0: tutconf = os.path.join(os.path.dirname(__file__), '/Users/tmc/Documents/LiveMesh/cherry/CherryPy-3.2.0/py2/cherrypy/tutorial/tutorial.conf') gyorgy@0: gyorgy@0: if __name__ == '__main__': gyorgy@0: # CherryPy always starts with app.root when trying to map request URIs gyorgy@0: # to objects, so we need to mount a request handler root. A request gyorgy@0: # to '/' will be mapped to HelloWorld().index(). gyorgy@0: cherrypy.quickstart(root, config=tutconf) gyorgy@0: else: gyorgy@0: # This branch is for the test suite; you can ignore it. gyorgy@0: cherrypy.tree.mount(root, config=tutconf) gyorgy@0: