python tornado框架学习

demo1:简单hello world

main.py

#!/usr/bin/env python
#-*-coding: utf-8-*-

# Version: 0.1
# Author: Song Huang <[email protected]>
# License: Copyright(c) 2015 Song.Huang
# Summary:

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options

define("port", default=8000, type=int)
#handler 类中的方法都是get、post、delete等
class IndexHandler(tornado.web.RequestHandler):
    def get(self):

        #get_argument 取函数方法
        res = self.get_argument('res', 'hello')

        #write 写到web页面上
        self.write(res + 'world!')


if __name__ == '__main__':

   #解析一个命令行
    tornado.options.parse_command_line()

    #路径匹配
    app = tornado.web.Application(
            handlers = [(r'/', IndexHandler)]
            )
   #  创建一个httpserver实例
    http_server = tornado.httpserver.HTTPServer(app)

    #监听端口
    http_server.listen(options.port)

    #启动项
    tornado.ioloop.IOLoop.instance().start()

-------------------------------------------------------------------------------------------------

url访问:

http://10.58.100.90:8000/

页面显示:hello world

http://10.58.100.90:8000/?res

页面显示:world!

--------------------------------------------------------

demo2:简单模板应用

main.py

#!/usr/bin/env python
#-*-coding: utf-8-*-

# Version: 0.1
# Author: Song Huang <[email protected]>
# License: Copyright(c) 2015 Song.Huang
# Summary:
import os.path

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):

        #渲染模板render
        self.render('index.html')

class ContentHandler(tornado.web.RequestHandler):
    def post(self):
        n1 = self.get_argument('n1')
        n2 = self.get_argument('n2')
        n3 = self.get_argument('n3')
        n4 = self.get_argument('n4')
        self.render('content.html', n1=n1, n2=n2, n3=n3,n4=n4)


if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = tornado.web.Application(
            handlers=[(r'/', IndexHandler), (r'/content', ContentHandler)],

            #模板路径
            template_path=os.path.join(os.path.dirname(__file__), "templates"),

            #静态文件路径 js、css
            static_path = os.path.join(os.path.dirname(__file__), "static")
            )

    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

templates文件夹下有另个文件 index.html content.html

index.html

<!DOCTYPE html>
<html>
    <head><title>Index</title></head>
    <body>

     //应用静态资源

      <href='static/1.css'>

       //快速加载静态资源

       <href='{{static_url("1.css")}}'>
       <form method="post" action="/content">
            <p>N1<br><input type="text" name="n1"></p>
            <p>N2<br><input type="text" name="n2"></p>
            <p>N3<br><input type="text" name="n3"></p>
            <p>N4<br><input type="text" name="n4"></p>
            <input type="submit">
        </form>
    </body>
</html>

content.html

<!DOCTYPE html>
<html>
    <head><title>Content</title></head>
    <body>
        <h1>n1: {{n1}}</h1>
        <h1>n2: {{n2}}</h1>
        <h1>n3: {{n3}}</h1>
        <h1>n4: {{n4}}</h1>
        {% if content %}
       
        {%end%}
        {%for i in content%}
       
        {%end%}
    </body>
</html>

----------------------------------------------------------------------------------

页面传参的方法{{}}和django类似

----------------------------------------------------------------------------------

demo3:

相关推荐