import tornado.web import tornado.ioloop import tornado.httpserver import tornado.options import config #全局參數定義 tornado.options.define("port",default=8000,type = int, help="run server on the given port.") tornado.options.define("itcast",default=[],type = str, multiple = True, help="itcast subjects.") #一個請求對應RequestHandler信息和方法 class IndexHandler(tornado.web.RequestHandler): #http請求方式get def get(self): self.write("Hello world") #http請求方式post def post(self): self.write("Hello world") if __name__ == '__main__': print(tornado.options.options.itcast) print(tornado.options.options.port) #建立web應用實例對象 請求對應一個IndexHandler類 app = tornado.web.Application([ (r"/",IndexHandler), ], **config.settings) # app.listen(8000) #建立服務器實例,綁定服務器端口 http_server = tornado.httpserver.HTTPServer(app) http_server.listen(tornado.options.options.port) #啓動當前線程的 tornado.ioloop.IOLoop.current().start()
config.pypython
import os # Tornado app配置 settings = { 'template_path': os.path.join(os.path.dirname(__file__), 'temp'), 'static_path': os.path.join(os.path.dirname(__file__), 'statics'), 'cookie_secret':'0Q1AKOKTQHqaa+N80XhYW7KCGskOUE2snCW06UIxXgI=', 'xsrf_cookies':False, 'login_url':'/login', 'debug':True, } # 日誌 log_path = os.path.join(os.path.dirname(__file__), 'logs/log')