猿问

根据URL动态路由到Flask操作

想要一个快速且肮脏的Flask测试服务器,可以在其中输入网址路径


0.0.0.0/**something**

并使其调用具有相同名称的相应方法。


像这样的东西:


from flask import Flask

app = Flask(__name__)


@app.route('/<action>')

def do_it(action=None):


    if {PSEUDO: The method exists, call it}

    else:

        return 'Action not found'


def something():


    return 'Did something'


if __name__ == '__main__':

    app.run()

Flask是否有帮助解决此问题的机制,还是我必须对反射感到混乱?


catspeake
浏览 227回答 2
2回答

慕标琳琳

不知道那有多凌乱,但似乎很简单:&nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; return globals()[action]()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; except KeyError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; return 'Action not found'&nbsp;
随时随地看视频慕课网APP

相关分类

Python
我要回答