Tornado Application的实现
作者:G_SANGSK 发布时间:2022-09-29 07:09:19
Application
-settings
我们在创建tornado.web.Application的对象时,传入了第一个参数——路由映射列表。实际上Application类的构造函数还接收很多关于tornado web应用的配置参数。
我们先来看一个参数:
debug,设置tornado是否工作在调试模式,默认为False即工作在生产模式。当设置debug=True 后,tornado会工作在调试/开发模式,在此种模式下,tornado为方便我们开发而提供了几种特性:
自动重启,tornado应用会监控我们的源代码文件,当有改动保存后便会重启程序,这可以减少我们手动重启程序的次数。需要注意的是,一旦我们保存的更改有错误,自动重启会导致程序报错而退出,从而需要我们保存修正错误后手动启动程序。这一特性也可单独通过autoreload=True设置;
取消缓存编译的模板,可以单独通过compiled_template_cache=False来设置;
取消缓存静态文件hash值,可以单独通过static_hash_cache=False来设置;
提供追踪信息,当RequestHandler或者其子类抛出一个异常而未被捕获后,会生成一个包含追踪信息的页面,可以单独通过serve_traceback=True来设置。
使用debug参数的方法:
import tornado.web
app = tornado.web.Application([], debug=True)
-路由映射
先前我们在构建路由映射列表的时候,使用的是二元元组,如:
[(r"/", IndexHandler),]
对于这个映射列表中的路由,实际上还可以传入多个信息,如:
[
(r"/", Indexhandler),
(r"/cpp", ItcastHandler, {"subject":"c++"}),
url(r"/python", ItcastHandler, {"subject":"python"}, name="python_url")
]
对于路由中的字典,会传入到对应的RequestHandler的initialize()方法中:
from tornado.web import RequestHandler
class ItcastHandler(RequestHandler):
def initialize(self, subject):
self.subject = subject
def get(self):
self.write(self.subject)
对于路由中的name字段,注意此时不能再使用元组,而应使用tornado.web.url来构建。name是给该路由起一个名字,可以通过调用RequestHandler.reverse_url(name)来获取该名子对应的url。
# coding:utf-8
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.options import options, define
from tornado.web import url, RequestHandler
define("port", default=8000, type=int, help="run server on the given port.")
class IndexHandler(RequestHandler):
def get(self):
python_url = self.reverse_url("python_url")
self.write('<a href="%s" rel="external nofollow" >itcast</a>' %
python_url)
class ItcastHandler(RequestHandler):
def initialize(self, subject):
self.subject = subject
def get(self):
self.write(self.subject)
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application([
(r"/", Indexhandler),
(r"/cpp", ItcastHandler, {"subject":"c++"}),
url(r"/python", ItcastHandler, {"subject":"python"}, name="python_url")
],
debug = True)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
来源:https://blog.csdn.net/G_SANGSK/article/details/82230250


猜你喜欢
- 1. void ellipse(InputOutputArray img, Po
- 前言最近使用Python解析IDX文件格式的MNIST数据集,需要对二进制文件进行读取操作,其中我使用的是struct模块。查了网上挺多教程
- 栈(Stack)在计算机领域是一个被广泛应用的集合,栈是线性集合,访问都严格地限制在一段,叫做顶(top)。 举个例子,栈就想一摞洗干净的盘
- 使用web.py做http server开发时,遇到postman能够正常请求到数据,但是浏览器无法请求到数据,查原因之后发现是跨域请求的问
- python去除字符串最后的换行符‘\n’s = s.replace('\n',
- 本文实例为大家分享了vue点击图片放大展示的具体代码,供大家参考,具体内容如下1.建立子组件,来实现图片方 * 能: BigImg.vue&l
- 什么是F型浏览?2006年4月,美国长期研究网站可用性的著名网站设计师杰柯柏·尼尔森(Jakob Nielsen)发表了一项《眼球轨迹的研究
- 简洁优雅的 C 写法:int a = 1; int b = 2; int temp; temp = a;&nb
- 废话不多说,大家直接看代码吧!"""遗传算法实现求函数极大值—Zjh"""imp
- Django配合python进行requests请求前言在我们写代码的时候,经常会用到前后端分离开发的方法,例如微信小程序,安卓,网站等等&
- PHP 中的 Interface 是一种非常重要的特性,它允许开发人员定义一组规范或者约束,以确保类之间的互操作性和兼容性。在本文中,我们将
- 很多时候我们需要让main函数不退出,让它在后台一直执行,例如:func main() { for i := 0;
- 创建Deque序列:from collections import dequed = deque()Deque提供了类似list的操作方法:
- 要下午传上的.结果事一多,忘记了.好不容易回来 . 这个和 dh20156 的那个,是差不多的。 找不到合适的图片,也
- Python使用for实现无限循环# 方法1.1:借助循环遍历列表的cycle方法from itertools import cyclefo
- 一、查看可用字体import pygameprint(pygame.font.get_fonts())二、设置字体1.使用系统字体self.
- 概况Vue3 里要实现数据的响应式监听一共有两种方式既:ref 和 reactive他们既有区别又有联系。ref()ref数据响应式监听。r
- linux系统中 linux默认的是utf8编码,而windows是gbk编码,所以会出现上面的乱码问题。 解决mysql导入导出数据乱码问
- 这篇文章主要介绍了python如何实现不可变字典inmutabledict,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 官方给出Vue.filters(id , [definition])//id {string}//definition {function}