tornado模板快速建立

快速建立tornado模板
server.py
import tornado.ioloop
import config
import tornado.httpserver

from application import Application
if name == “main”:
app = Application()

httpServer = tornado.httpserver.HTTPServer(app)
httpServer.bind(config.options.get("port"))
httpServer.start(1)
# httpServer.listen(8000)
# 参数app告诉httpServer 路由
# app.listen(8000)仅能在单进程中使用
tornado.ioloop.IOLoop.current().start()

config.py
import os
BASE_DIRS = os.path.dirname(file)

options = {
“port”: 6546,
“list”: [“good”, “handsome”, “nice”]
}

# 配置
settings = {
“static_path”: os.path.join(BASE_DIRS, “static”),
“templates_path”: os.path.join(BASE_DIRS, “templates”),
“debug”: True
}
application.py
import tornado.web
from views import index
import config

class Application(tornado.web.Application):
def init(self):
handlers = [
(r’/nice’, index.IndexHandler),
(r’/home’, index.HomeHandler),
(r’/big’, index.hjjHandler),
tornado.web.url(r’/jiege’, index.jiegeHandler, {“word3”: “handsome”, “word4”: “cool”}, name=“jiegejj”)
]
super(Application, self).init(handlers, **config.settings)

index.py
from tornado.web import RequestHandler

class IndexHandler(RequestHandler):
def get(self, *args, **kwargs):
url = self.reverse_url(“jiegejj”)
self.write(“跳转”%(url))

class HomeHandler(RequestHandler):
def get(self, *args, **kwargs):
self.write(“gua pi chenbin”)

class hjjHandler(RequestHandler):
def get(self, *args, **kwargs):

    self.write("hjj is a nice man")

class jiegeHandler(RequestHandler):
def initialize(self, word3, word4):
self.word3 = word3
self.word4 = word4

def get(self, *args, **kwargs):

self.write(“hjj is handsome”)
inint.py
将写好的文件按照如图放入文件夹中
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值