教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python tornado如何处理http请求?

python tornado如何处理http请求?

发布时间:2021-01-10   编辑:jiaochengji.com
教程集为您提供python tornado如何处理http请求?等资源,欢迎您收藏本站,我们将为您提供最新的python tornado如何处理http请求?资源

tornado作为python中的框架,能处理异步之类的问题,是网页方面比较重要的组成部分。小编查阅了相关资料,找到了一些python tornado处理http请求的内容,分享给大家。


实现下列方法来处理HTTP请求:

RequestHandler.get(args,kwargs) #处理get请求

RequestHandler.post(*args, kwargs) #处理post请求

RequestHandler.put(args, kwargs) #处理put请求

RequestHandler.delete(*args, kwargs) #处理delete请求

 

Hello world

新建一个hello.py写入以下内容

import tornado.ioloop
import tornado.web
 
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello world")
 
class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/', MainHandler),
        ]
        tornado.web.Application.__init__(self, handlers)
 
if __name__=="__main__":
    app = Application()
    app.listen(8000)
    print("Tornado Started in port 8000,http://127.0.0.1:8000")
tornado.ioloop.IOLoop.current().start()

 

执行该文件python3 hello.py

当你看到控制台输出:

Tornado Started in port 8000http://127.0.0.1:8000

既运行成功,此时浏览器访问127.0.0.1:8000就可以看到Hello world

这个Hello world创建了一个socket服务并监听8000端口,当接收到请求时根据路由规则来找到相应的类处理该请求,并根据请求方式指定相应类中的指定方法处理。 所以当我们在浏览器键入127.0.0.1:8000,服务端会给浏览器返回Hello world

以上就是python tornado处理http请求的方法更多Python学习推荐:JQ教程网Python大全

您可能感兴趣的文章:
python tornado如何启动和配置?
python tornado如何处理http请求?
python tornado视图是什么?怎么用?
tornado与django区别
flask与tornado哪个简单
python tornado是什么?怎么用?
python web为什么不火
Python web目前较火的三大框架
django怎么处理高并发
django框架怎么实现高并发

[关闭]
~ ~