You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:使用Azure Python SDK获取App Service常规设置信息

获取Azure App Service运行时栈版本(PHP/Python/Java)的解决方案

要验证App Service的运行时栈配置(比如PHP 8.2、Python/Java版本),你需要获取App Service的站点配置信息,而非仅通过列表接口返回的基础数据。以下是具体实现方案:

Python SDK(azure-mgmt-web)实现

使用azure-mgmt-web的get_configuration方法可获取包含运行时栈的完整站点配置,步骤如下:

  1. 初始化WebSiteManagementClient
  2. 调用web_apps.get_configuration方法传入资源组和App Service名称
  3. 从返回的配置对象中提取运行时栈信息

代码示例

from azure.identity import DefaultAzureCredential
from azure.mgmt.web import WebSiteManagementClient

# 初始化客户端
subscription_id = "你的订阅ID"
credential = DefaultAzureCredential()
web_client = WebSiteManagementClient(credential, subscription_id)

# 指定资源组和App Service名称
resource_group_name = "你的资源组名"
app_service_name = "你的App Service名称"

# 获取站点配置
config = web_client.web_apps.get_configuration(resource_group_name, app_service_name)

# 解析运行时栈信息(区分Linux和Windows)
if config.linux_fx_version:
    # Linux环境格式:例如 PHP|8.2、PYTHON|3.11、JAVA|17-java17
    stack, version = config.linux_fx_version.split("|")
    print(f"运行时栈: {stack}, 版本: {version}")
elif config.windows_fx_version:
    # Windows环境格式:例如 PHP|8.2、PYTHON|3.11、JAVA|17
    stack, version = config.windows_fx_version.split("|")
    print(f"运行时栈: {stack}, 版本: {version}")
else:
    print("未检测到指定的运行时栈配置")

关键说明

  • linux_fx_version/windows_fx_version字段存储了运行时栈的完整信息,格式为栈名称|版本号
  • 若需批量查询资源组下所有App Service,可先通过list_by_resource_group获取所有App名称,再循环调用get_configuration

Java SDK实现

使用Azure Java SDK的azure-mgmt-web包,同样通过获取站点配置来提取运行时栈信息:

代码示例

import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.appservice.models.WebApp;

public class AppServiceStackChecker {
    public static void main(String[] args) {
        // 初始化Azure资源管理器
        AzureResourceManager azure = AzureResourceManager.configure()
                .authenticate(new DefaultAzureCredentialBuilder().build())
                .withSubscription("你的订阅ID");

        // 指定资源组和App Service名称
        String resourceGroupName = "你的资源组名";
        String appServiceName = "你的App Service名称";

        // 获取WebApp并提取配置
        WebApp webApp = azure.webApps().getByResourceGroup(resourceGroupName, appServiceName);
        String linuxFxVersion = webApp.linuxFxVersion();
        String windowsFxVersion = webApp.windowsFxVersion();

        // 解析运行时栈
        if (linuxFxVersion != null && !linuxFxVersion.isEmpty()) {
            String[] parts = linuxFxVersion.split("\\|");
            System.out.printf("运行时栈: %s, 版本: %s%n", parts[0], parts[1]);
        } else if (windowsFxVersion != null && !windowsFxVersion.isEmpty()) {
            String[] parts = windowsFxVersion.split("\\|");
            System.out.printf("运行时栈: %s, 版本: %s%n", parts[0], parts[1]);
        } else {
            System.out.println("未检测到指定的运行时栈配置");
        }
    }
}

内容的提问来源于stack exchange,提问作者Huu Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:10:06