tornado api 简单模板

Posted 星空守望者--jkmiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tornado api 简单模板相关的知识,希望对你有一定的参考价值。

# 安装

pip insatll tornado

 

# 主文件 web_server.py

#!/usr/bin/env python
# encoding: utf-8

import tornado.httpserver
import tornado.options
import tornado.web
from tornado.options import define,options
import sys,os,re
from apicode import Apicode

reload(sys)
sys.setdefaultencoding(utf-8)


# 定义默认调用端口为8088
define("port",default=8088,help="run on the given port",type=int)

# 验证码演示demo
class VcodeHandler(tornado.web.RequestHandler):
    # get 请求
    def get(self):
        result = self.get_argument("result",{})
        city = self.get_argument(city, u重庆)
        self.render("input_code.html", city=city, result=result)

    # post 请求
    def post(self):
        
        result = {code:0 , msg: success, result: ‘‘}
        
        # 获取参数
        city = self.get_argument(city, CQ)
        method = self.get_argument(method, LR)
        detail = eval(self.get_argument(detail, False))
        imgUrl = self.get_argument(imgUrl, ‘‘)
        imgFile = self.request.files.get(imgfile, [])
        fname = ‘‘

        # url 方式
        if imgUrl and re.search(.+\.(jpg|png|bmp|gif), imgUrl):
            fname = ./static/uploads/%s % imgUrl.split(/)[-1]
            result[result]= vcode.predict_url(city, imgUrl, detail, method)
       
        # 上传文件方式
        elif imgFile:
            for img in imgFile:
                with open(./static/uploads/ + img[filename], wb) as fw:
                    fw.write(img[body])
                fname = ./static/uploads/ + img[filename]
                result[result] = vcode.predict(city, fname, detail, method)       
        else:
            errorMsg = "上传验证码图片文件错误或url图片格式不正确"
            result[code] = -1
            result[msg] = errorMsg

        self.render("output_code.html", city=city, source=fname, detail=detail, method=method, result=result)

# 验证码调用api
class VcodeApiHandler(tornado.web.RequestHandler):

    def get(self):
        result = {code: 1000 , msg: 调用参数错误, 请用post方式请求, city & imgfile 参数, result: 使用说明: http://gitlab.tangees.com/miaoweihong/verify-code}
        json_result = tornado.escape.json_encode(result)
        self.write(json_result)

    def post(self):
        
        result = {code: 0 , msg: success, result: ‘‘}
        city = self.get_argument(city, CQ)
        method = self.get_argument(method, LR)
        detail = eval(self.get_argument(detail, False))

        imgFile = self.request.files.get(imgfile, [])
        if imgFile:
            for img in imgFile:
                with open(./static/uploads/ + img[filename], wb) as fw:
                    fw.write(img[body])
                fname = ./static/uploads/ + img[filename]
                try:
                    result[result] = vcode.predict(city, fname, detail, method)
                except Exception as e:
                    result[code] = 1001
                    result[msg] = 上传文件内容有误 + str(e)
        else:
            result[code] = 1002
            result[msg] = 没有文件内容
        # 返回json结果
        json_result = tornado.escape.json_encode(result)
        self.write(json_result)


class TestHandler(tornado.web.RequestHandler):
    def get(self):
        self.write(hello, jkmiao)


if __name__ == "__main__":
    
    # 引入自行定义的模块
    vcode = Apicode() 
    tornado.options.parse_command_line()
    
    app = tornado.web.Application(
        handlers = [(r/vcode,VcodeHandler), (r/vcodeapi, VcodeApiHandler)], # url匹配
        template_path = os.path.join(os.path.dirname(__file__),"templates"),  # 定义视图页面地址,放 html文件
        static_path = os.path.join(os.path.dirname(__file__), "static"),   # 定义静态模板,放 css,js等文件
        debug=True,   # 是否为debug模式
        autoescape=None, # 不设置转义字符
        )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    print "starting tornado at port http://127.0.0.1:%d" % options.port
    tornado.ioloop.IOLoop.instance().start()

搞定!

 

以上是关于tornado api 简单模板的主要内容,如果未能解决你的问题,请参考以下文章

python SSTI tornado render模板注入

有没有一种好方法可以将 pychecker/pylint 应用于 Tornado 模板中的 python 代码?

2018护网杯easy_tornado(SSTI tornado render模板注入)

1.python web框架tornado之web框架简单介绍模板语言介绍

认识tornado

Python大神教你300行代码搞定HTML模板渲染「附源码」