MAC安装配置Tornado

MAC安装配置Tornado

下载文件

下载python,可以根据个人需要下载python2.x或者python3.x,下载tornado安装包。

开始安装

安装python和tornado

这里以python3.x为例,下载安装包完成后,点击安装python3.x,然后命令行运行,可以

cd tornado
python setup.py build
sudo python setup.py install

测试

编一个测试的py文件,hello.py,内容如下:

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 + ', tornado!')

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()

加执行权限后,执行python3.6 ./hello.py 启动服务器。

在本地浏览器打开http://localhost:8000/可以看到:
Hello, tornado!

你可能感兴趣的:(后台)