zoukankan      html  css  js  c++  java
  • Tornado

    Tornado

    Tornado是使用Python编写的一个强大的、可扩展的Web服务器。它在处理严峻的网络流量时表现得足够强健,但却在创建和编写时有着足够的轻量级,并能够被用在大量的应用和工具中。

    windows安装

    c:> pip install tornado

    实例:

    创建一个.py文件

    import tornado.ioloop
    import tornado.web
    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("Hello, world")
    
    application = tornado.web.Application([
        (r"/index", MainHandler),
    ])
    if __name__ == "__main__":
        application.listen(8888)
        tornado.ioloop.IOLoop.instance().start()
    访问

    第一步:执行脚本,监听 8888 端口

    第二步:浏览器客户端访问 /index  -->  http://127.0.0.1:8888/index

    第三步:服务器接受请求,并交由对应的类处理该请求

    第四步:类接受到请求之后,根据请求方式(post / get / delete ...)的不同调用并执行相应的方法

    第五步:方法返回值的字符串内容发送浏览器

    路由系统

    路由系统其实就是 url 和 类 的对应关系,这里不同于其他框架,其他很多框架均是 url 对应 函数,Tornado中每个url对应的是一个类。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
      
    import tornado.ioloop
    import tornado.web
      
      
    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("Hello, world")
      
    class StoryHandler(tornado.web.RequestHandler):
        def get(self, story_id):
            self.write("You requested the story " + story_id)
      
    class BuyHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("buy.wupeiqi.com/index")
      
    application = tornado.web.Application([
        (r"/index", MainHandler),
        (r"/story/([0-9]+)", StoryHandler),
    ])
      
    application.add_handlers('buy.wupeiqi.com$', [
        (r'/index',BuyHandler),
    ])
      
    if __name__ == "__main__":
        application.listen(80)
        tornado.ioloop.IOLoop.instance().start()

    配置模板,静态路径

    目录:

    代码:

    app.py
    =============
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    import tornado.ioloop
    import tornado.web
    from controller.home import MainHandler
    
    settings = {
    	'template_path': 'views',
    	'static_path': 'static',
    }
    application = tornado.web.Application([
    	(r"/index", MainHandler),
    ], **settings)
    
    
    if __name__ == "__main__":
    	application.listen(8888)
    	tornado.ioloop.IOLoop.instance().start()
    
    
    index.html
    ===========
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <h1>asdfsadf</h1>
        <form action="/index" method="POST">
            <input type="text" name="user" />
            <input type="text" name="pwd" />
            <input type="submit" value="提交" />
        </form>
        <!--<img src="/static/1.jpg">-->
        <hr/>
        <ul>
        {% for item in user_info_list%}
            <li>{{item.get('u', "123")}}-{{item['p']}}</li>
        {% end %}
        </ul>
    </body>
    </html>
    
    
    home.py
    ============
    import tornado.web
    user_info = []
    class MainHandler(tornado.web.RequestHandler):
    	def get(self):
    		# self.write("Hello, world") # HttpResponse
    
    		self.render('index.html', user_info_list = user_info)
    
    	def post(self, *args, **kwargs):
    		# self.write('post')
    		# self.render('index.html')
    		# 获取用户提交的数据
    		user = self.get_argument('user')
    		pwd = self.get_argument('pwd')
    		user_info.append({'u': user, 'p': pwd})
    		self.redirect('/index')
    

    访问:

     

  • 相关阅读:
    JavaScript中运算符的优先级
    JS中在当前日期上追加一天或者获取上一个月和下一个月
    Window命令行工具操作文件
    多线程Worker初尝试
    基于gulp的前端自动化开发构建新
    cURL和file_get_contents实现模拟post请求
    Thinkphp5使用validate实现验证功能
    微信小程序wx.pageScrollTo的替代方案
    js设计模式之代理模式以及订阅发布模式
    js设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/Z-style/p/6008986.html
Copyright © 2011-2022 走看看