【发布时间】:2020-01-15 00:42:36
【问题描述】:
使用 Python 3.8 和 Flask,我需要下面的服务器端代码:
1) 将未知长度的端点列表传递给 app.route(),
2) 通过打印添加的路线,确保它们被列为有效路线。
以下代码没有错误,但似乎没有做任何事情。
# server.py
from flask import Flask
methods = ['GET','POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH']
endpoints = ['users','countries', <etc>] # <etc> to indicate list of unknown length
app = Flask(__name__)
for endpoint in endpoints:
#print(endpoint)
app.route('/<str:endpoint>', methods=methods)
print(app.url_map)
if __name__ == '__main__':
app.run()
print(app.url_map) 只返回:
Map([<Rule '/static/<filename>' (GET, HEAD, OPTIONS) -> static>]) ...我认为这意味着我列出的端点都没有被添加!
使用上面代码的以下 3 个请求,只有前 2 个的端点是有效的:
'http://127.0.0.1:5000/users',
'http://127.0.0.1:5000/countries',
'http://127.0.0.1:5000/xxx'
没有列出客户端代码,因为它与上述问题无关。
【问题讨论】: