Understanding the root demo
-
Start the root demo by running the command:
- simple_server.py --factory quixote.demo.create_publisher
In a browser, open http://localhost:8080 as before. Click around at will.
This is the default demo, but it is more complicated than the mini_demo described above. The create_publisher() function in quixote.demo.__init__.py creates a publisher whose root directory is an instance of quixote.demo.root.RootDirectory. Note that the source code is a file named "root.ptl". The suffix of "ptl" indicates that it is a PTL file, and the import must follow a call to quixote.enable_ptl() or else the source file will not be found or compiled. The quixote.demo.__init__.py file takes care of that.
Take a look at the source code in root.ptl. You will see code that looks like regular python, except that some function definitions have "[html]" between the function name and the parameter list. These functions are ptl templates. For details about PTL, see the PTL.txt file.
This RootDirectory class is similar to the one in mini_demo.py, in that it has a _q_index() method and '' appears in the _q_exports list. One new feature here is the presence of a tuple in the _q_exports list. Most of the time, the elements of the _q_exports lists are just strings that name attributes that should be available as URL components. This pattern does not work, however, when the particular URL component you want to use includes characters (like '.') that can't appear in Python attribute names. To work around these cases, the _q_exports list may contain tuples such as ("favicon.ico", "favicon_ico") to designate "favicon_ico" as the attribute name corresponding the the "favicon.ico" URL component.
Looking at the RootDirectoryMethods, including plain(), css() and favon_ico(), you will see examples where, in addition to returning a string containing the body of the HTTP response, the function also makes side-effect modifications to the response object itself, to set the content type and the expiration time for the response. Most of the time, these direct modifications to the response are not needed. When they are, though, the get_response() function gives you direct access to the response instance.
The RootDirectory here also sets an 'extras' attribute to be an instance of ExtraDirectory, imported from the quixote.demo.extras module. Note that 'extras' also appears in the _q_exports list. This is the ordinary way to extend your URL space through another '/'. For example, the URL path '/extras/' will result in a call to the ExtraDirectory instance's _q_index() method.
今天看的是demo.html的第三部分内容: Understanding the root demo。
在命令行输入:
simple_server.py --factory quixote.demo.create_publisher
在浏览器输入:
http://localhost:8080
我们看到了这样的页面:
Hello, world!
To understand what's going on here, be sure to read the doc/demo.txt
file included with Quixote.
Here are some features of this demo:
- simple: A Python function that generates a very simple document.
- plain: A Python function that generates a plain text document.
- error: A Python function that raises an exception.
- publish_error: A Python function that raises a
PublishError
exception. This exception will be caught by a_q_exception_handler
method. - dumpreq: Print out the contents of the HTTPRequest object.
- css: The stylesheet for this document.
- extras/: Demos of some of Quixote's more advanced features.
__init__.py的内容很简单,是mini_demo.py的一部分:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
root.ptl的内容也和mini_demo.py大部分类似,区别在几个地方:
- 有些函数名和参数列表之间有一个”[html]”,表示这些函数是ptl模板。
- _q_exports中出现了一个tuple,用来转化带”.“的路径组件,因为Python的属性名里是不能出现”."的。
- 有些方法例如plain()、css()和favicon_ico,他们修改reponse对象自身,而不是返回一个包含HTTP body的字符串。
- RootDirectory类的一个属性extras = ExtraDirectory(),这将调用ExtraDirectory的一个实例。具体的在下一部分里解释。
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
这两条语句不可少。