1.1Tornado是什么?
Tornado是使用python编写的一个强大的,可扩展的web服务器。他在处理严峻的网络流量是表现的足够强健,但却在创建和编写时有着足够的轻量级,并能够被用在大量的应用和工具中。
底层基于协程+epoll
1.1.1 Tornado入门
简单编写一个Hello Word
hello.py
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.options
class IndexHandlenr(tornado.web.RequestHandler):
def get(self):
self.write('hello word')
if __name__ == '__main__':
appliectin = tornado.web.Application([
(r'/',IndexHandlenr)
])
appliectin.listen(8003)
tornado.ioloop.IOLoop.current().start()
你可以在命令行里尝试运行这个程序已测试输出:
python hello.py --port=8003
现在你可以在浏览器中打开http://localhost:8003,或者打开另一个终端窗口使用curl测试我们的应用:
$ curl http://localhost:8000/
Hello, friendly user!
$ curl http://localhost:8000/?greeting=Salutations
Salutations, friendly user!