Flask Restful的子路径

时间:2018-12-16 17:58:02

标签: flask flask-restful

我想提供我用Flask-Restful编写的带有子路径的api。本质上,我希望在“ http://localhost:5000/flask”中找到应用程序的根目录。

我尝试使用SCRIPT_NAME进行以下操作:

class FixScriptName(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        SCRIPT_NAME = '/flask'

        if environ['PATH_INFO'].startswith(SCRIPT_NAME):
            environ['PATH_INFO'] = environ['PATH_INFO'][len(SCRIPT_NAME):]
            environ['SCRIPT_NAME'] = SCRIPT_NAME
            return self.app(environ, start_response)
        else:
            start_response('404', [('Content-Type', 'text/plain')])
            return ["This doesn't get served by your FixScriptName middleware.".encode()] 

然后我通过从run_simple运行werkzeug.serving启动服务器进行开发:

run_simple('0.0.0.0', 5000, app, use_reloader=True)

如果我访问任何资源,都会收到301错误。

我定义的示例资源是:

api.add_resource(UserRegister, '/register/')

0 个答案:

没有答案