推薦一python
推薦二web
# hello world import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") def make_app(): return tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": app = make_app() app.listen(8888) tornado.ioloop.IOLoop.current().start()
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()