1. 程式人生 > >Tornado之hello world

Tornado之hello world

import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.options
from tornado.options import define, options

#定義埠配置
define('port', type=int, default=8080)

#建立檢視處理器
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("<h1>hello,world</h1>")

#建立路由表
urls = [(r"/", MainHandler),]

#建立配置-開啟除錯模式
configs = dict(debug=True)
#自定義應用
class MyApplication(tornado.web.Application):
    def __init__(self, urls, configs):
        super(MyApplication, self).__init__(handlers=urls, **configs)
#建立伺服器
def make_app():
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(MyApplication(urls,configs))
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

#啟動伺服器
if __name__ == '__main__':
    make_app()