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.py
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')