Flask:如何以编程方式重启 Azure 应用



我有4只Scrapy蜘蛛,我通过Azure上的Flask启动。如何通过单击我网站上的按钮重新启动应用程序?如何在烧瓶函数中使用 REST API?

重新启动:

<a href="/restart" class="btn btn-danger">Restart</a>

瓶:

@app.route('/restart')
def restart():
# REST API

如果要重启 Azure Web 应用,请按照以下步骤操作:

1.安装以下 python 软件包:

azure-mgmt-resource 和 azure-mgmt-web.

2.然后创建用于身份验证的服务主体。可以使用 Azure CLI 或 Azure 门户来创建它。下面是使用 azure cli 的示例:

az ad sp create-for-rbac --name xxxx

在输出中,您可以获取这些项目,并将它们写下来:

application id(client id)
directory id(tenant)
client secret(secret)

然后使用以下代码:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.web import WebSiteManagementClient
subscription_id ="xxxx" #you can get it from azure portal
client_id ="xxx"
secret="xxx"
tenant="xxx"
credentials = ServicePrincipalCredentials(
client_id= client_id,
secret=secret,
tenant = tenant
)
#resource_client = ResourceManagementClient(credentials,subscription_id)
web_client = WebSiteManagementClient(credentials,subscription_id)
#restart your azure web app
web_client.web_apps.restart("your_resourceGroup_name","your_web_app_name")

最新更新