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())
标签:__,Web,框架,Tornado,self,db,blog,options,define
From: https://www.cnblogs.com/leo130-blogs/p/18287460