Python Tornado框架轻松写一个Web应用的全过程
作者:雷学委 时间:2022-05-10 10:38:36
目录
Tornado是什么
安装
试试看使用tornado框架来写一个web application
总结
Tornado是什么
学委之前在看Jupyter组件的源码的时候,发现了tornado这个web框架。
不仅仅做一个web框架, 通过使用非阻塞网络I/O,Tornado可以扩展到数万个开放连接。
这样非常适合 long polling , WebSockets 以及其他需要与每个用户建立长期连接的应用程序。
好,下面安装试用一下。
安装
pip install tornado
pip 不会用的朋友可以看看 # 秒懂精通pip并快速体验深度学习应用吧!【多图建议收藏】
试试看使用tornado框架来写一个web application
来一个简单的,保存下面代码为app.py文件
"""
雷学委#代码Demo
"""
import tornado.ioloop
import tornado.web
“”“编写一个请求处理器”“”
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, Tornado Server");
"""创建一个web app"""
def make_app():
#绑定接口“/” 到MainHandler
return tornado.web.Application([
(r"/", MainHandler),
])
#程序入口
if __name__ == '__main__':
app = make_app()
app.listen(8080)
tornado.ioloop.IOLoop.current().start()
运行如下:(python app.py)
效果如下:
这个示例很简单,先学会用起来。
再试试做一个form
"""
雷学委#代码Demo
"""
import tornado.ioloop
import tornado.web
#一个处理form的请求处理器
class MyFormHandler(tornado.web.RequestHandler):
def get(self):
#直接页面渲染一个form
self.write('<html><body class="leiXueWei"><form action="/form" method="POST">'
'<input type="text" name="message">'
'<input class="leiXueWei" type="submit" value="Submit">'
'</form></body></html>')
def post(self):
self.set_header("Content-Type", "text/plain")
self.write("You wrote " + self.get_body_argument("message"))
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, Tornado Server");
def make_xue_wei_app():
#使用application加载多个路由: “/” 接口路由 和 “/form” 接口路由
return tornado.web.Application([
(r"/", MainHandler),
(r"/form", MyFormHandler)
])
if __name__ == '__main__':
app = make_xue_wei_app()
app.listen(18081)
tornado.ioloop.IOLoop.current().start()
以上代码比前面第一个示例多了一个form的请求处理器和路由加载。
看看页面渲染的效果,没有内嵌任何js,这个submit按钮就直接把发起了POST请求给“/form”接口。
而且博主也没有编写任何JS处理页面,特别简化,这个”小魔法“是本文想要推荐tornado的一个原因!
以上随笔,简单使用分享,后面再继续推这个框架的解读。
参考链接:
关于Pip的使用 => https://www.jb51.net/article/219385.htm
Tornado => www.osgeo.cn/tornado/
来源:https://juejin.cn/post/6994600768573538341
标签:tornado,框架,web应用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python列表添加元素append(),extend(),insert(),+list的区别及说明
2022-12-11 08:48:35
![](https://img.aspxhome.com/file/2023/1/95161_0s.png)
firefox通过XUL实现text-overflow:ellipsis的效果
2008-07-08 19:12:00
pycharm进入时每次都是insert模式的解决方式
2023-08-09 09:52:16
![](https://img.aspxhome.com/file/2023/6/62906_0s.jpg)
php checkbox复选框值的获取与checkbox默认值输出方法
2023-11-14 13:41:12
JSP组件commons-fileupload实现文件上传
2023-07-03 03:16:01
php+ajax无刷新上传图片实例代码
2023-11-17 11:27:58
![](https://img.aspxhome.com/file/2023/1/104181_0s.png)
删除数组中重复项(uniq)
2009-12-28 12:23:00
Web设计色彩速查表
2009-12-21 16:24:00
chatGPT之Python API启用上下文管理案例详解
2022-09-21 13:51:43
HTML在线编辑器任意设置字号大小
2007-08-29 19:55:00
使用MySql和php出现中文乱码的解决方法
2009-01-14 12:02:00
什么样的图标更具有可用性
2007-10-16 17:47:00
![](https://img.aspxhome.com/file/UploadPic/200710/16/20071016174919899s.jpg)
python中的元组与列表及元组的更改
2023-10-19 07:28:04
python基于gevent实现并发下载器代码实例
2024-01-01 00:31:53
Python为人脸照片添加口罩实战
2021-11-12 23:39:33
![](https://img.aspxhome.com/file/2023/3/67313_0s.jpg)
ASP自动清除ACCESS数据库的日文字符
2007-11-28 17:40:00
python利用matplotlib库绘制饼图的方法示例
2022-12-17 15:16:50
![](https://img.aspxhome.com/file/2023/3/97263_0s.png)
python虚拟环境迁移方法
2021-09-13 07:52:51
![](https://img.aspxhome.com/file/2023/0/69000_0s.jpg)
Python threading Local()函数用法案例详解
2021-11-27 21:57:02
![](https://img.aspxhome.com/file/2023/4/72944_0s.jpg)
python实现FTP文件传输的方法(服务器端和客户端)
2021-03-29 23:36:08
![](https://img.aspxhome.com/file/2023/9/65979_0s.png)