Tornado合集
一、目录结构
- static 静态资源文件
- templates html模板文件夹
- upfile 上传文件夹
- views 视图文件夹
-- __init__.py
-- index.py index界面
- app.py 主服务
- application.py 路由映射
- config.py tornado配置
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7bec9e605b55c95b9f5ad9162b664507.png)
二、各文件代码
2.1 views/index.py
from tornado.web import RequestHandler
# 业务处理类
class IndexHandler(RequestHandler):
def get(self,*args,**kwargs):
self.write("hello world")
2.2 app.py
import tornado.httpserver
import tornado.web
import tornado.ioloop
# 导入配置
import config
# 导入路由映射
from application import Application
app = Application()
httpServer = tornado.httpserver.HTTPServer(app)
httpServer.bind(config.options['port'])
httpServer.start()
if __name__ == '__main__':
tornado.ioloop.IOLoop.current().start()
2.3 application.py
import tornado.web
from views import index
import config
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/',index.IndexHandler)
]
# 调用父类Application,传递handlers,传递配置
super(Application,self).__init__(handlers,**config.settings)
2.4 config.py
import os
# 当前文件的绝对路径
BASE_DIRS = os.path.dirname(__file__)
# 参数
options = {
"port":8000
}
# 配置
settings = {
"static_path":os.path.join(BASE_DIRS,'static'),
"template_path":os.path.join(BASE_DIRS,'templates'),
"debug":True
}