zoukankan      html  css  js  c++  java
  • web开发框架之 Tornado

    在server.py文件中

    import tornado.web import tornado.ioloop import tornado.httpserver import config
    from views import index from application import Application app = Application() if __name__ == '__main__': # 创建服务 # app.listen(8080) # app.listen(config.options['port']) # 自己创建一个服务 httpServer = tornado.httpserver.HTTPServer(app) # 给该服务绑定一个端口 httpServer.bind(config.options['port']) # 开启一个进程 httpServer.start(1) # 启动服务,并监听 tornado.ioloop.IOLoop.current().start()
    在config.py文件中  专门用来 存放和配置信息有关的
    import
    os # 获取当前目录下的根目录 BASE_DIR = os.path.dirname(__file__) # 参数 options = { "port": 8000, } IMAGES_PATH = "./image/test" # 配置 settings = { "static_path": os.path.join(BASE_DIR, "static"), "template_path": os.path.join(BASE_DIR, "templates"), # debug为True是调试模式,如果为False的是生产模式 和Django这点相同 "debug": True, # 为True的特性: # 取消缓存编译的模板 compiled_template_cache = False # 取消缓存静态文件的hash值 static_hash_cache = False # 提供追踪信息 serve_traceback = True # 自动重启 autoreload=True # self.redirect 重定向 # self.send_error(state_code=500) # self.write_error(state_code=500) # self.reverse_url() 反向解析 # tornado.web.RequestHandler # 利用http协议向服务端传递参数 # 提取url特定参数,get/post传递参数,既可以获取get请求也可以获取post请求,在http的报文头中增加自定义的参数 # self.get_query_argument() 获取get请求时url中的参数 # self.get_query_arguments() 获取get请求时url中的参数两个以上同名的返回值为一个list # request对象 # tornado.httputil.HTTPFile对象 } print("当前的根目录为") print(BASE_DIR) print("当前静态文件存储路径为") print(settings["static_path"]) print("当前模板文件的存储路径为") print(settings["template_path"])
    在appication.py中存放和路由有关
    import
    tornado.web from views import index, login import config class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", index.IndexHandler), (r"/sunck", index.SunckHandler, {"word1": "good", "word2": "nice"}), (r"/login", login.LoginHandler), (r"/json", index.JsonHandler), (r"/json2", index.JsonsHandler) ] super(Application, self).__init__(handlers, **config.settings) print(config.settings) # super(Application, self).__init__(handlers, autoreload=True)
    在 index.py中存放和路由有关的首页视图集
    import
    tornado.web import json import os import base64 class IndexHandler(tornado.web.RequestHandler): def get(self): self.write("hello world") class SunckHandler(tornado.web.RequestHandler): # 获取路由中的参数,并传给http请求方式,进行处理 def initialize(self, word1, word2): self.word1 = word1 self.word2 = word2 def get(self): print(self.word1, self.word2) self.write("sunck....") class JsonHandler(tornado.web.RequestHandler): def get(self): per = { "name": "张三", "age": 23, "classes": "初二(1)班", } # 将Python数据类型转换为字符串 json_str = json.dumps(per) self.set_header("Content-Type","Application/json") self.write(json_str) class JsonsHandler(tornado.web.RequestHandler): def get(self): per = { "name": "张三", "age": 23, "classes": "初二(1)班", "height": 78, } self.write(per) # 重定向到首页 class RedirectHandler(tornado.web.RequestHandler): def get(self): self.redirect("/") self.write("redirect......")
  • 相关阅读:
    2.17NOIP模拟赛(by hzwer) T2 小奇的序列
    2.17NOIP模拟赛(by hzwer) T1 小奇挖矿
    题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S
    题解【CF886B】Vlad and Cafes
    题解【CJOJ1070/UVA】嵌套矩形
    题解 【CF381A】 Sereja and Dima
    何时使用UI层的智能表单技术
    开机加电到系统打开究竟发生了什么?(1)
    asp.net MVC 常见安全问题及解决方案
    HDU 4422 The Little Girl who Picks Mushrooms【水题】
  • 原文地址:https://www.cnblogs.com/wangxiongbing/p/11003223.html
Copyright © 2011-2022 走看看