# 安装

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()

搞定!

教程

===============最基本  hello world ==================

代码清单:hello.py

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web from tornado.options import define, options
define("port", default=, help="run on the given port", type=int) class IndexHandler(tornado.web.RequestHandler):
def get(self):
greeting = self.get_argument('greeting', 'Hello')
self.write(greeting + ', friendly user!') if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

tornado api 简单模板的更多相关文章

  1. Libvlc API 简单说明 [转]

    Libvlc API 简单说明 原文来自http://www.xuebuyuan.com/1519616.html libvlc_instance_t* libvlc_new(int argc, co ...

  2. tornado 学习笔记9 Tornado web 框架---模板(template)功能分析

            Tornado模板系统是将模板编译成Python代码.         最基本的使用方式: t = template.Template("<html>{{ myv ...

  3. vs 2013下自定义ASP.net MVC 5/Web API 2 模板(T4 视图模板/控制器模板)

    vs 2013下自定义ASP.net MVC 5/Web API 2  模板(T4 视图模板/控制器模板): Customizing ASP.NET MVC 5/Web API 2 Scaffoldi ...

  4. 再起航,我的学习笔记之JavaScript设计模式30(简单模板模式)

    简单模板模式 概念介绍 简单模板模式(Simple template): 通过格式化字符串拼凑出视图避免创建视图时大量节点操作,优化内存开销. 创建模板 在实际的业务中如果我们需要进行前后台交互,或多 ...

  5. salesforce零基础学习(八十五)streaming api 简单使用(接近实时获取你需要跟踪的数据的更新消息状态)

    Streaming API参考链接: https://trailhead.salesforce.com/en/modules/api_basics/units/api_basics_streaming ...

  6. 基于C语言libvirt API简单小程序

    libvirt API简单小程序 1.程序代码如下 #include<stdio.h> #include<libvirt/libvirt.h> int getDomainInf ...

  7. helm-chart-2-chart结构和简单模板

    1, chart 结构介绍 我们创建一个chart 并查看其结构 右侧注释为其文件的的解释 $ helm create mychart $ cd mychart/ $ tree ├── charts ...

  8. maven pom文件简单模板和配置详解

    https://blog.csdn.net/earbao/article/details/49924943 maven pom文件简单模板和配置详解

  9. day8学python 各种简单模板

    各种简单模板 内容: 1.shelve模板 存储数据 2.shutil 模板 用作拷贝/删除/压缩文件(使用便捷) 3.hashlib 模板 加密文件 4.re模板 ================= ...

随机推荐

  1. C++ STL--stack/queue 的使用方法

    1.stack stack 模板类的定义在<stack>头文件中.stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类 ...

  2. 第一章Android系统移植与驱动开发概述--读书笔记

    以前,初步学习过嵌入式Linux驱动开发的基础课程,对于驱动开发可以说是有了一点点微末的基础吧.首先我们要对Android嵌入式系统有一个初步的认识,Android系统发展到今天已经具备了完善的架构. ...

  3. 电脑用miniDP链接显示器后电脑没声音

    今天用笔记本T440s miniDP 链接戴尔U2515显示器的 DP 连接后发现笔记本没声音了 原因: miniDP 不仅能支持视频传输还支持音频, 所以声音就改为从显示器发出了 但是我想让电脑输出 ...

  4. MCMC: The Metropolis Sampler

    本文主要译自 MCMC: The Metropolis Sampler 正如之前的文章讨论的,我们可以用一个马尔可夫链来对目标分布 \(p(x)\) 进行采样,通常情况下对于很多分布 \(p(x)\) ...

  5. 吴奇隆刘诗诗婚礼场地:巴厘岛Ayana酒店,美到窒息!

    导读:忍不住转载一下,原文地址:http://www.sjq315.com/news/270768.html 3月20日,吴奇隆和刘诗诗在巴厘岛五星级酒店Ayana Resort and Spa酒店举 ...

  6. CGRectInset & CGRectOffset

    1.CGRectInset CGRect CGRectInset (   CGRect rect,   CGFloat dx,   CGFloat dy);以原rect为中心,再参考dx,dy,进行缩 ...

  7. CRT 和mysql 中文乱码解决方式

    mysql 安装mysql 1. 使用root用户: su root 2. 安装 yum install mysql yum install mysql-server yum install mysq ...

  8. Psam_ISO7816

    ISO7816协议1-4部分下载

  9. jQuery中的map()方法

    jQuery中map()方法的使用格式为:$(selector).map(callback(index,domElement)). 将在每一个被选元素上执行map()方法中设置的回调函数,在回调函数中 ...

  10. 移动端bug~~移动端:active伪类无效的解决方法【移动端 :active样式无效】

    移动端:active伪类无效的解决方法[移动端 :active样式无效]2016-09-26  15:46:50 问题: 移动端开发的时候实现按钮的点击样式变化,但是在iphone[safiri Mo ...