安装tornado
git clone https://github.com/tornadoweb/tornado.git
python setup.py build #编译安装程序 sudo python setup.py install #安装
可能会需要安装依赖:
sudo apt-get install build-essential python-dev
测试是否安装成功:
import tornado
如果没有报错,就说明已经安装成功了
第一个Tornado程序
import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web from tornado.options import define, options define("port", default=8000, help="run on the given port", type=int) class IndexHandler(tornado.web.RequestHandler): def get(self): greeting = self.get_argument('greeting', 'Hello') self.write(greeting + ', friendly user!') if __name__ == "__main__": tornado.options.parse_command_line() app = tornado.web.Application(handlers=[(r"/", IndexHandler)]) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
运行:
python hello.py --port=8000 curl http://127.0.0.1:8000 #可以查看web返回情况 #如果不希望打印日志,加上参数--logging=none
[dm href=’http://demo.pythoner.com/itt2zh/ch1.html#ch1-1-2′]参考地址-Tornado目录-Hello Tornado[/dm]