gpt4 book ai didi

python-3.x - Flask-restplus API中如何将API改为域名?

转载 作者:行者123 更新时间:2023-12-03 16:10:03 25 4
gpt4 key购买 nike

我正在尝试将我的 flask api 转发到 mydomain.example,终点是 api.mydomain.example

例如,我的方法 ping 将有一个终点 api.mydomain.example/v1/server/ping。但是,我得到的是 xx.xxx.xxx.xx:5005/v1/server/ping 作为端点。

查看 SO 中的其他问题,我发现了修改 app.config['SERVER_NAME'] 或将 subdomain 添加到 route 的建议装饰器。但没有任何效果。 (当我进行任何这些修改时,我会收到 404 错误)。

这是一个最小的工作示例:

from flask import Flask, Blueprint
from flask_restplus import Resource, Api

app = Flask(__name__)
api = Api(app, version='1.0',
title='My API',
description='An API')

blueprint = Blueprint('api', __name__, url_prefix='/v1')
api.init_app(blueprint)
app.register_blueprint(blueprint)
#app.config['SERVER_NAME'] = 'mydomain.example:5005'
#app.url_map.default_subdomain = "test"

server = api.namespace("server",
description='Server API')


@server.route("/ping") #, subdomain="test")
class Ping(Resource):
def get(self):
"""
Check if Server is still alive
"""
return {"reply":"PONG"}

if __name__ == '__main__':
app.config["SWAGGER_UI_DOC_EXPANSION"] = "list"
app.run(port=5005, host= '0.0.0.0', debug=True)

我转到了我的域管理页面 (name.com) 并添加了 url 转发。在这种情况下,mydomain.info 会转到 swagger 管理页面,但 mydomain.example/v1/server/ping 也会转到 swagger 管理页面。

但是,我仍然在请求 URL 部分中得到 xx.xxx.xxx.xx:5005。

如何让它与子域名一起使用?

最佳答案

您不必更改应用配置。您需要做的是在 WSGI 服务器上运行您的应用程序,该服务器将通过 Apache 和 Nginx 等 Web 服务器代理您的应用程序(例如在端口 80 上)。 WSGI 服务器需要一个入口点才能在您指定的端口(代码段中的 5005)上与您的应用程序通信。 Here's一个相当简单的教程(有关更多详细信息,您还可以引用 this 链接)。

此外,API 网关是您最好的 friend ,尤其是当您将 API 发布到公共(public)互联网时。你可以看看AWS API GatewayApigee用于 API 管理,具有对 Swagger 的一流支持(还有许多其他支持 - 包括 OSS)。

关于python-3.x - Flask-restplus API中如何将API改为域名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50317168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com