## page was renamed from CheeryPyTut/04 complex site.py ## page was renamed from CheerPyTut/04 complex site.py ##language:zh {{{#!python # -*- coding: gbk -*- """ Tutorial 04 - 多媒对象 本教程展示如何组织一个完整的站点,通过不同对象的继承关系 """ from cherrypy import cpg # 自个儿加的,为了尝试外部文件的引入 import os,sys class HomePage: def __init__(self): # 嗯嗯!初始化中,读入一个有表单的HTML self.login = open("login.html","r").read() def index(self): return '''
Hi, this is the home page! Check out the other fun stuff on this site:
"In Python, how do you create a string of random characters?" -- "Read a Perl file!"
[Return]
''' index.exposed = True class LinksPage: """每个对象可以处理嵌套的对象请求 - 最简单的方式是在各自的 __init__ 函式中声明 """ def __init__(self): self.extra = ExtraLinksPage() def index(self): """我们可以随意指向外部链接,对象并不会在意链接是否在站内 """ return '''Here are some useful links:
You can check out some extra useful links here.
[Return]
''' index.exposed = True class ExtraLinksPage: def index(self): # 注意相对路径可以安全的被识别处理 return '''Here are some extra useful links:
''' index.exposed = True # Of course we can also mount request handler objects right here! # 当然我们可以集中的在此声明所有发布对象 cpg.root = HomePage() cpg.root.joke = JokePage() cpg.root.links = LinksPage() # 注意,你不必在此装载 ExtraLinksPage 因为 LinksPage并不在此初始化 # 实际上,你没有理由要在 根对象中关心下层对象的解析 # 所有的处理,会在URL请求时自动进行的! cpg.server.start(configFile = 'tutorial.conf') }}}