一、注释app.listen()
二、导入 tornado.httpserver
import tornado.web
import tornado.ioloop
import tornado.httpserver
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write('fuck off 假如我能中大奖!')
if __name__ == '__main__':
app = tornado.web.Application(
[
(r'/', IndexHandler)
]
)
# app.listen(8888)
# 手动创建服务器
# HTTPServer(app)中的app是为了让服务器到app中匹配路由,并只能匹配app中路由,
# 若客户端输入app中没有的路由则报错
httpServer = tornado.httpserver.HTTPServer(app)
# httpServer.listen和app.listen不一样,两个listen表示不一样的对象,就好比小明有10元,小红也有10元,对象不同
# app.listen()等同于 httpServer = tornado.httpserver.HTTPServer(app)+httpServer.listen()
# 使用 httpServer = tornado.httpserver.HTTPServer(app)+httpServer.listen()该方法是更加直观的如何启动服务器
httpServer.listen(8888)
tornado.ioloop.IOLoop.current().start()
三、运行