python - 从 Flask 中的另一个 API 多次调用一个 API

标签 python flask

我是 Flask 新手,我想从另一个 API(比如 A)多次调用一个 API(比如 B)。为了更清楚地解释,当 A 被调用时我得到一个项目列表。其中每一项都需要完成一项任务 (B)。 B 已经写好了,所以我想,为什么不在 A 中得到的列表中的每个项目上调用 B 呢?就在那时我遇到了redirect 。但在我看到的所有例子中,它都是用在最后的 return 中。语句使其有效地重定向到另一个 URL 1 次。

我想做的是:

# In API A
status = []
for item in items:
    status.append(<some_way_to_call_B>(item))    # status will contain the responses of every API call
return Jsonify(status)    # front-end shows which were successful and which weren't

可能吗?

最佳答案

您可以使用 python requests 调用端点 B 内的端点 A

例如:

@app.route("/A"):
def somefuncA():
    #some stuff to do
    return jsonify(result)

@app.route("/B"):
def somefuncB():

    #some stuff to do
    response = requests.get("<IP>/A")
    #use the result as desired

    return jsonify(result)

关于python - 从 Flask 中的另一个 API 多次调用一个 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59246613/

相关文章:

python - 如何在 python 中将 EOF 写入 STDIN popen

python - 第一次 Facebook 登录后如何存储用户名(在数据库中)?

python - Flask-Migrate 未检测到表格

http-status-code-404 - flask :蓝图的error_handler

python - Flask 中同一路由中不同类型的 POST 请求

python - 修改 python nltk.word_tokenize 以排除 "#"作为分隔符

python - Argparse 通过分组互斥

python - 从登录我的 GAE 应用程序的用户发送电子邮件

python - kubernetes 收到 502 错误网关

python - Flask 重定向 url 与 anchor