我遇到的问题是当我运行一个类似于下面这样的 tornado
的 hello-world 示例:
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=9999, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
greeting = self.get_argument('greeting', 'Hello')
self.write(greeting + ', friendly user!')
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/hello", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
我运行了这段代码,并执行了以下命令:
curl http://localhost:9999/hello
,它返回了200的HTTP状态码。但是,当我在路径末尾添加斜线并执行命令
curl http://localhost:9999/hello/
时,返回了404的HTTP状态码。我知道问题可能出在代码中的这一行:
app = tornado.web.Application(handlers=[(r"/hello", IndexHandler)])
我想知道是否有一种简单的方法来修复 http://localhost:9999/hello
和 http://localhost:9999/hello/
两个地址的访问问题。
同时,我也很想了解路径末尾是否加斜杆(/)的区别,比如上面的http://localhost:9999/hello
和 http://localhost:9999/hello/
,或者有时我们上传文件时会遇到这种情况。
reverse_url()
输出将包括尾随的?
)。我在谷歌搜索中找到了这个答案来解决这个反向 URL 问题。 :) - kitti