Home Backend Development Python Tutorial 高性能web服务器框架Tornado简单实现restful接口及开发实例

高性能web服务器框架Tornado简单实现restful接口及开发实例

Jun 16, 2016 am 08:43 AM
tornado web framework

有个朋友让我搞搞tornado框架,说实话,这个框架我用的不多。。。

我就把自己的一些个运维研发相关的例子,分享给大家。

怎么安装tornado,我想大家都懂。

pip install tornado
Copy after login

再来说说他的一些个模块,官网有介绍的。我这里再啰嗦的复读机一下,里面掺夹我的理解。

主要模块
web - FriendFeed 使用的基础 Web 框架,包含了 Tornado 的大多数重要的功能,反正你进入就对了。
escape - XHTML, JSON, URL 的编码/解码方法
database - 对 MySQLdb 的简单封装,使其更容易使用,是个orm的东西。
template - 基于 Python 的 web 模板系统,类似jinja2
httpclient - 非阻塞式 HTTP 客户端,它被设计用来和 web 及 httpserver 协同工作,这个类似加个urllib2
auth - 第三方认证的实现(包括 Google OpenID/OAuth、Facebook Platform、Yahoo BBAuth、FriendFeed OpenID/OAuth、Twitter OAuth)
locale - 针对本地化和翻译的支持
options - 命令行和配置文件解析工具,针对服务器环境做了优化,接受参数的

底层模块
httpserver - 服务于 web 模块的一个非常简单的 HTTP 服务器的实现
iostream - 对非阻塞式的 socket 的简单封装,以方便常用读写操作
ioloop - 核心的 I/O 循环

再来说说tornado接受请求的方式:
关于get的方式

class MainHandler(tornado.web.RequestHandler): 
  def get(self): 
    self.write("You requested the main page") 
class niubi(tornado.web.RequestHandler): 
  def get(self, story_id): 
    self.write("xiaorui.cc niubi'id is " + story_id) 
application = tornado.web.Application([ 
  (r"/", MainHandler), 
  (r"/niubi/([0-9]+)", niubi), 
])

Copy after login

这样我们访问 /niubi/123123123 就会走niubi这个类,里面的get参数。
关于post的方式

class MainHandler(tornado.web.RequestHandler): 
  def get(self): 
    self.write('<html><body><form action="/" method="post">'
         '<input type="text" name="message">'
         '<input type="submit" value="Submit">'
         '</form></body></html>') 
  def post(self): 
    self.set_header("Content-Type", "text/plain") 
    self.write("xiaorui.cc and " + self.get_argument("message"))

Copy after login

在tornado里面,一般get和post都在一个访问路由里面的,只是按照不同method来区分相应的。
扯淡的完了,大家测试下get和post。

import tornado.ioloop 
import tornado.web 
import json 
class hello(tornado.web.RequestHandler): 
  def get(self): 
    self.write('Hello,xiaorui.cc') 
class add(tornado.web.RequestHandler): 
  def post(self): 
    res = Add(json.loads(self.request.body)) 
    self.write(json.dumps(res)) 
def Add(input): 
  sum = input['num1'] + input['num2'] 
  result = {} 
  result['sum'] = sum 
  return result 
application = tornado.web.Application([ 
  (r"/", hello), 
  (r"/add", add), 
]) 
if __name__ == "__main__": 
  application.listen(8888) 
  tornado.ioloop.IOLoop.instance().start() 
Copy after login

#大家可以写个form测试,也可以用curl -d测试

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Build web applications using Golang's web framework Buffalo Build web applications using Golang's web framework Buffalo Jun 24, 2023 am 10:27 AM

Buffalo is a web framework developed using Golang that provides a solution for rapid development of web applications. In this article, we will introduce how to use Buffalo to build a web application. Installing Buffalo First, we need to install Buffalo locally. Buffalo provides a convenient command line tool through which you can build and run applications. Before installing, make sure you have Golang and Node.js installed. Ran

Share a Nodejs web framework: Fastify Share a Nodejs web framework: Fastify Aug 04, 2022 pm 09:23 PM

This article will share with you a Nodejs web framework: Fastify. It will briefly introduce the features supported by Fastify, the plug-ins supported by Fastify, and how to use Fastify. I hope it will be helpful to everyone!

Go language web framework comparison: gin vs. echo vs. iris Go language web framework comparison: gin vs. echo vs. iris Jun 17, 2023 pm 07:44 PM

As the demand for web development continues to increase, web frameworks in various languages ​​​​are gradually diversified, and the Go language is no exception. Among the many web frameworks in Go language, gin, echo and iris are the three most popular frameworks. In this article, we will compare the pros and cons of these three frameworks to help you choose the right one for your project. Gingin is a lightweight web framework that features high performance and flexibility. It supports middleware and routing functionality, which makes it ideal for building RESTful

How to implement a lightweight web framework using PHP and Slim How to implement a lightweight web framework using PHP and Slim Jun 25, 2023 pm 01:03 PM

Web frameworks have become an integral part of modern web application development, providing an infrastructure that enables developers to create and deploy their applications faster. In PHP development, Slim is a lightweight web framework known for its ease of use and rapid development. This article will show you how to create a simple but powerful web application using PHP and Slim. What is Slim? Slim is a lightweight web framework written in the language PHP. Its core

Solution to NotImplementedError() Solution to NotImplementedError() Mar 01, 2024 pm 03:10 PM

The reason for the error is in python. The reason why NotImplementedError() is thrown in Tornado may be because an abstract method or interface is not implemented. These methods or interfaces are declared in the parent class but not implemented in the child class. Subclasses need to implement these methods or interfaces to work properly. How to solve this problem is to implement the abstract method or interface declared by the parent class in the child class. If you are using a class to inherit from another class and you see this error, you should implement all the abstract methods declared in the parent class in the child class. If you are using an interface and you see this error, you should implement all methods declared in the interface in the class that implements the interface. If you are not sure which

Web framework and web service development in Go language Web framework and web service development in Go language Jun 03, 2023 am 08:02 AM

Go language has become increasingly popular in the field of web development in recent years. On the one hand, its performance and concurrency characteristics are excellent, and it is very suitable for handling highly concurrent Web requests; on the other hand, its development efficiency has gradually improved, and more and more Web frameworks and development tools have been launched. This article will mainly introduce the relevant content of developing web frameworks and web services in Go language. Whether you are a beginner in web development or a developer with some experience, you can learn about the relevant knowledge and techniques of web development in Go language through this article.

Quickly build web applications using Golang's web framework beego Quickly build web applications using Golang's web framework beego Jun 24, 2023 am 11:22 AM

With the development and popularization of Internet technology, there are more and more demands for Web applications. Building Web applications quickly and efficiently has become an urgent need for developers. Golang's dynamic characteristics, efficient execution capabilities and rich web frameworks have become the first choice for many developers. Among the many Golang web frameworks, beego is a fast, concise, efficient and easy-to-use web framework. It relies on Go's native HTTP package, has RESTful support, MVC mode, and comes with ORM and

Implementing API gateway using Golang's web framework Iris framework Implementing API gateway using Golang's web framework Iris framework Jun 24, 2023 am 11:24 AM

An API gateway is a network service used to manage and route API (Application Programming Interface) requests. It is an intermediary that receives client requests and forwards them to the backend service. The benefit of an API gateway is that it can provide a consistent interface for multiple services and provide features such as security and monitoring. In this article, we will implement an API gateway using Golang's web framework Iris framework. The Iris framework is a high-performance web framework that is designed to be simple, fast, easily scalable and maintainable. Iris box

See all articles