tornado

Python框架之Tornado(三)请求

大憨熊 提交于 2020-03-15 09:54:49
概述 本篇就来详细介绍tornado服务器(socket服务端)是如何接收用户请求数据以及如果根据用户请求的URL处理并返回数据,也就是上图的3系列所有步骤,如上图【start】是一个死循环,其中利用epoll监听服务端socket句柄,一旦客户端发送请求,则立即调用HttpServer对象的_handle_events方法来进行请求的处理。 对于整个3系列按照功能可以划分为四大部分: 获取用户请求数据(上图3.4) 根据用户请求URL进行路由匹配,从而使得某个方法处理具体的请求(上图3.5~3.19) 将处理后的数据返回给客户端(上图3.21~3.23) 关闭客户端socket(上图3.24~3.26) 3.1、HTTPServer对象的_handle_events方法 此处代码主要有三项任务:   1、 socket.accept() 接收了客户端请求。   2、创建封装了客户端socket对象和IOLoop对象的IOStream实例(用于之后获取或输出数据)。   3、创建HTTPConnection对象,其内容是实现整个功能的逻辑。 class HTTPServer(object): def _handle_events(self, fd, events): while True: try: #======== 获取客户端请求 =========# connection,

tornado解析http body的过程分析

放肆的年华 提交于 2020-03-13 16:52:03
tornado解析http body的过程分析 在最近写的一个RESTful API Server过程中,发现tornaod对解析POST BODY的内容有限制。 而在以前用web.py则没有这个限制,使用httplib2作为客户端。 客户端代码: def request(self, url, method, **kwargs): request_kwargs = copy.copy(kwargs) request_kwargs.setdefault('headers', kwargs.get('headers', {})) request_kwargs['headers']['Accept'] = 'application/json' request_kwargs['headers']['Content-type'] = 'application/x-www-form-urlencoded' try: request_kwargs['body']['token'] = self.token request_kwargs['body'] = urllib.urlencode(kwargs['body']) except KeyError: pass resp, body = super(HTTPClient, self).request(self.api_url + url,

Tornado——将数据发送到前端

可紊 提交于 2020-03-13 16:51:17
将数据发送到前端 1.write(chunk) 将chunk数据写到输出缓冲区。之前一直用的这种方式,例如: class IndexHandler(RequestHandler): def get(self): self.write("hello,world1!") # 可以在同一个处理方法中多次使用write方法 self.write("hello,world2!") self.write("hello,world3!") write方法是写到缓冲区的,我们可以像写文件一样多次使用write方法不断追加响应内容,最终所有写到缓冲区的内容一起作为本次请求的响应输出。 将json数据传到前端: import json class IndexHandler(RequestHandler): def get(self): stu = { "name":"zhangsan", "age":24, "gender":1, } stu_json = json.dumps(stu) self.write(stu_json) 实际上,我们可以不用自己手动去做json序列化,当write方法检测到我们传入的chunk参数是字典类型后,会自动帮我们转换为json字符串。 class IndexHandler(RequestHandler): def get(self): stu = { "name":

python web框架之 tornado

﹥>﹥吖頭↗ 提交于 2020-03-13 16:37:27
在server.py文件中import tornado.web import tornado.ioloop import tornado.httpserver import config from views import index from application import Application app = Application() if __name__ == '__main__': # 创建服务 # app.listen(8080) # app.listen(config.options['port']) # 自己创建一个服务 httpServer = tornado.httpserver.HTTPServer(app) # 给该服务绑定一个端口 httpServer.bind(config.options['port']) # 开启一个进程 httpServer.start(1) # 启动服务,并监听 tornado.ioloop.IOLoop.current().start() 在config.py文件中 专门用来 存放和配置信息有关的import os # 获取当前目录下的根目录 BASE_DIR = os.path.dirname(__file__) # 参数 options = { "port": 8000, } IMAGES_PATH = ".

Flask Tornado 简单对比

南笙酒味 提交于 2020-03-07 23:52:35
Tornado 的使用和 Flask 相差不远,但是,对于 Tornado 的最大的特点——异步却是其他框架所不能及的,本文就以个人的见解出发,对比一下两款 Python 的流行框架:Flask 和 Tornado。 对于一个 Web 框架来说,我觉得有几个方面是需要注意的,分别是: 路由 请求和响应方式 session 和 cookie 模板 扩展性 下面我就以这些方面为切入点对比一下这两款框架:Flask == 0.11.1, Tornado == 4.4.2 路由 Flask 在路由方面选择比较多,常用的有两种方式,一种是 blueprint 方式,还有一种就是类的方式,而 Tornado 就只有一种类的方式,下面就以类的方式分别列举一下两种框架的写法: Flask View 的类 from flask.views import View class ShowUsers(View): def dispatch_request(self): users = User.query.all() return render_template('users.html', objects=users) app.add_url_rule('/users/', view_func=ShowUsers.as_view('show_users')) 而 Tornado 中是这样使用的:

Tornado doesn't work asynchronously

天大地大妈咪最大 提交于 2020-03-06 05:28:49
问题 I am trying to write a non-blocking api with tornado. But when I try it on local the first request is blocking the API. I tried to use different browsers but the result is same. I opened chrome and firefox. On chrome I go http://localhost:8888/test-first and while it is loading I immediatly go http://localhost:8888/test-second from firefox. I am expecting the request from firefox would be answered while the other one is still loading. But both of them are waiting and when the first request is

Tornado doesn't work asynchronously

蓝咒 提交于 2020-03-06 05:28:48
问题 I am trying to write a non-blocking api with tornado. But when I try it on local the first request is blocking the API. I tried to use different browsers but the result is same. I opened chrome and firefox. On chrome I go http://localhost:8888/test-first and while it is loading I immediatly go http://localhost:8888/test-second from firefox. I am expecting the request from firefox would be answered while the other one is still loading. But both of them are waiting and when the first request is

客户端与服务器持续同步解析(轮询,comet,WebSocket)

廉价感情. 提交于 2020-03-05 15:57:31
在B/S模型的Web应用中,客户端常常需要保持和服务器的持续更新。这种对及时性要求比较高的应用比如:股票价格的查询,实时的商品价格,自动更新的twitter timeline以及基于浏览器的聊天系统(如GTalk)等等。由于近些年AJAX技术的兴起,也出现了多种实现方式。本文将对这几种方式进行说明,并用jQuery+tornado进行演示,需要说明的是,如果对tornado不了解也没有任何问题,由于tornado的代码非常清晰且易懂,选择tornado是因为其是一个非阻塞的(Non-blocking IO)异步框架(本文使用2.0版本)。 在开始之前,为了让大家有个清晰的认识,首先列出本文所要讲到的内容大概。本文将会分以下几部分: 普通的轮询(Polling) Comet :基于服务器长连接的“服务器推”技术。这其中又分为两种: 基于AJAX和基于IFrame的 流(streaming)方式 。 基于AJAX的 长轮询(long-polling)方式 。 WebSocket 古老的轮询 轮询最简单也最容易实现,每隔一段时间向服务器发送查询,有更新再触发相关事件。对于前端,使用js的setInterval以AJAX或者JSONP的方式定期向服务器发送request。 var polling = function(){ $.post('/polling', function(data,

Tornado

 ̄綄美尐妖づ 提交于 2020-03-04 00:47:23
Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本。这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了能有效利用非阻塞式服务器环境,这个 Web 框架还包含了一些相关的有用工具 和优化。 Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的框架)有着明显的区别:它是非阻塞式服务器,而且速度相当快。得利于其 非阻塞的方式和对 epoll 的运用,Tornado 每秒可以处理数以千计的连接,这意味着对于实时 Web 服务来说,Tornado 是一个理想的 Web 框架。我们开发这个 Web 服务器的主要目的就是为了处理 FriendFeed 的实时功能 ——在 FriendFeed 的应用里每一个活动用户都会保持着一个服务器连接。(关于如何扩容 服务器,以处理数以千计的客户端的连接的问题,请参阅 C10K problem 。) pip install tornado 源码安装 https: / / pypi.python.org / packages / source / t / tornado / tornado - 4.3 .tar.gz 一、快速上手 第一步:执行脚本,监听xxxx端口 第二步:浏览器客户端访问 /index --> http://127.0.0

bottle

匆匆过客 提交于 2020-03-03 22:18:20
Bottle是一个快速、简洁、轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块。 1 pip install bottle 2 easy_install bottle 3 apt-get install python-bottle 4 wget http://bottlepy.org/bottle.py 安装 Bottle框架大致可以分为以下部分: 路由系统,将不同请求交由指定函数处理 模板系统,将模板中的特殊语法渲染成字符串,值得一说的是Bottle的模板引擎可以任意指定:Bottle内置模板、 mako 、 jinja2 、 cheetah 公共组件,用于提供处理请求相关的信息,如:表单数据、cookies、请求头等 服务,Bottle默认支持多种基于WSGI的服务 1 server_names = { 2 'cgi': CGIServer, 3 'flup': FlupFCGIServer, 4 'wsgiref': WSGIRefServer, 5 'waitress': WaitressServer, 6 'cherrypy': CherryPyServer, 7 'paste': PasteServer, 8 'fapws3': FapwsServer, 9 'tornado':