1
2 """
3 Tutorial 04 - 多媒对象
4
5 本教程展示如何组织一个完整的站点,通过不同对象的继承关系
6 """
7 from cherrypy import cpg
8
9 import os,sys
10
11 class HomePage:
12 def __init__(self):
13
14 self.login = open("login.html","r").read()
15 def index(self):
16 return '''
17 <p>Hi, this is the home page! Check out the other
18 fun stuff on this site:</p>
19
20 <ul>
21 <li><a href="/joke/">A silly joke</a></li>
22 <li><a href="/links/">Useful links</a></li>
23 </ul>
24 <hr/>
25 %s
26 '''%self.login
27
28 index.exposed = True
29
30 def doLogin(self, username=None, password=None):
31
32 return '''用户:%s
33 <br/>
34 口令:%s
35 '''%(username,password)
36 doLogin.exposed = True
37
38 class JokePage:
39 def index(self):
40 return '''
41 <p>"In Python, how do you create a string of random
42 characters?" -- "Read a Perl file!"</p>
43 <p>[<a href="../">Return</a>]</p>
44 '''
45 index.exposed = True
46
47
48 class LinksPage:
49 """每个对象可以处理嵌套的对象请求
50 - 最简单的方式是在各自的 __init__ 函式中声明
51 """
52 def __init__(self):
53 self.extra = ExtraLinksPage()
54
55 def index(self):
56 """我们可以随意指向外部链接,对象并不会在意链接是否在站内
57 """
58 return '''
59 <p>Here are some useful links:</p>
60
61 <ul>
62 <li><a href="http://www.cherrypy.org">The CherryPy Homepage</a></li>
63 <li><a href="http://www.python.org">The Python Homepage</a></li>
64 </ul>
65
66 <p>You can check out some extra useful
67 links <a href="./extra/">here</a>.</p>
68
69 <p>[<a href="../">Return</a>]</p>
70 '''
71
72 index.exposed = True
73
74
75 class ExtraLinksPage:
76 def index(self):
77
78 return '''
79 <p>Here are some extra useful links:</p>
80
81 <ul>
82 <li><a href="http://del.icio.us">del.icio.us</a></li>
83 <li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
84 </ul>
85
86 <p>[<a href="../">Return to links page</a>]</p>
87 '''
88
89 index.exposed = True
90
91
92
93
94 cpg.root = HomePage()
95 cpg.root.joke = JokePage()
96 cpg.root.links = LinksPage()
97
98
99
100
101
102 cpg.server.start(configFile = 'tutorial.conf')
CherryPyTut/04_complex_site.py (last edited 2009-12-25 07:16:41 by localhost)