为什么python tornado请求不显示在浏览器中?

2024-10-01 02:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试运行简单的tornado代码。但在浏览器中看不到任何内容。若一切正常,我将看到函数的响应。 我使用安装tornado的旧版本

pip install tornado==4.5.3

我运行代码并尝试在浏览器中使用

http://localhost:8826/A
http://localhost:8826/B

控制台打印

in __main__ 01
in __main__ 02
in __main__ 03
in __main__ 04
in __main__ 05

我的代码

import nest_asyncio

nest_asyncio.apply()

import tornado.web
from tornado.ioloop import IOLoop
from tornado import gen

from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor   # `pip install futures` for python2



class TestHandler01(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)

    @tornado.gen.coroutine
    def get(self):
        print("in TestHandler01 get")
        self.write('Response from server01')

class TestHandler02(tornado.web.RequestHandler):
    @gen.coroutine
    def get(self):
        print("in TestHandler02 get")
        self.write('Response from server02')
        self.finish()

if __name__ == '__main__':
    print("in __main__ 01")
    application = tornado.web.Application([
        (r"/A", TestHandler01),
        (r"/B", TestHandler02),
        ])
    print("in __main__ 02")
    application.listen(8826)
    print("in __main__ 03")

    IOLoop.instance().stop()
    print("in __main__ 04")
    IOLoop.instance().start()
    print("in __main__ 05")

Tags: 代码infromimportselfwebgetmain