• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
leo130-blogs
博客园    首页    新随笔    联系   管理    订阅  订阅

Tornado Web框架学习(一)

tornado框架
1、单文件下对应用的配置
2、简单的get请求测试
3、错误重定向测试

import tornado
import asyncio

from os import path
from tornado.options import define,options

# define database and server config
# top level

define("port",default=8888,help="ServerPort",type=int)
# define("db_host", default="127.0.0.1", help="blog database host")
# define("db_port", default=5432, help="blog database port")
# define("db_database", default="blog", help="blog database name")
# define("db_user", default="blog", help="blog database user")
# define("db_password", default="blog", help="blog database password")

class Application(tornado.web.Application):
    def __init__(self,*db):
        # self.db = db
        handlers = [
            (r"/Home",HomeHandler),
            (r"/Get_param_test",GettestHandle),
            (r"/error_redirect_test",errorRedirectTest),
            
        ]
        settings = dict(
            blog_title="Tornado Blog",
            template_path=path.join(path.dirname(__file__), "templates"),
            static_path=path.join(path.dirname(__file__), "static"),
            ui_modules={"Entry": EntryModule},
            xsrf_cookies=True,
            # cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
            # login_url="/auth/login",
            debug=True,
        )
        super().__init__(handlers, **settings)

class EntryModule:
    pass

class noResultError(Exception):
    pass

class BaseHandler(tornado.web.RequestHandler):
    pass

class HomeHandler(BaseHandler):
    async def get(self):
        return self.write( f"this is Home Page")
    
class GettestHandle(BaseHandler):
    async def get(self):
        param = self.get_query_argument("param")
        return self.write(f"I get a param:{param}")

class errorRedirectTest(BaseHandler):
    async def get(self):
        try:
            raise noResultError("manual error")
        except noResultError:
            return self.redirect("/Home")
            

async def main():
    tornado.options.parse_command_line()

    # Create the global connection pool.
    # async with aiopg.create_pool(
    #     host=options.db_host,
    #     port=options.db_port,
    #     user=options.db_user,
    #     password=options.db_password,
    #     dbname=options.db_database,
    # ) as db:
    #     await maybe_create_tables(db)
    #     app = Application(db)
    #     app.listen(options.port)

    #     # In this demo the server will simply run until interrupted
    #     # with Ctrl-C, but if you want to shut down more gracefully,
    #     # call shutdown_event.set().
    #     shutdown_event = tornado.locks.Event()
    #     await shutdown_event.wait()
    app = Application()
    app.listen(options.port)
    shutdown_event = tornado.locks.Event()
    await shutdown_event.wait()


if __name__ == "__main__":
    asyncio.run(main())
posted @ 2024-07-06 16:54  Sanchez023  阅读(15)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3