Azure Web Management Client Library

10.1.0 · active · verified Thu Apr 09

The `azure-mgmt-web` library provides a Pythonic interface for managing Azure App Services, Azure Functions, and other web-related resources within your Azure subscription. It is part of the Azure SDK for Python, allowing programmatic control over deployments, scaling, configurations, and monitoring of web applications. The current version is 10.1.0, and new versions are released regularly, often aligning with updates to the Azure REST API and broader Azure SDK guidelines.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate using `DefaultAzureCredential` and then list all App Service Plans within your Azure subscription using the `WebSiteManagementClient`. Ensure your `AZURE_SUBSCRIPTION_ID` environment variable is set and you're authenticated (e.g., via `az login`).

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

# Ensure AZURE_SUBSCRIPTION_ID is set in your environment variables,
# or replace 'YOUR_SUBSCRIPTION_ID' directly. Make sure you are also
# logged in via Azure CLI ('az login') or have other AZURE_IDENTITY
# environment variables configured for DefaultAzureCredential to work.

# Retrieve Azure Subscription ID from environment variable
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")

if subscription_id == "YOUR_SUBSCRIPTION_ID":
    print("WARNING: Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'YOUR_SUBSCRIPTION_ID'.")
    # Exit or raise error if no subscription ID is available for a real run
    # exit(1)

try:
    # Authenticate with Azure. DefaultAzureCredential attempts several common auth methods.
    credential = DefaultAzureCredential()

    # Create a WebSiteManagementClient instance
    web_client = WebSiteManagementClient(credential, subscription_id)

    print(f"Listing App Service Plans in subscription: {subscription_id}")

    # List all App Service Plans in the subscription
    app_service_plans = web_client.app_service_plans.list()

    found_plans = False
    for plan in app_service_plans:
        print(f"- Name: {plan.name}, Resource Group: {plan.resource_group_name}, Location: {plan.location}")
        found_plans = True
    
    if not found_plans:
        print("No App Service Plans found or you may not have access to any.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure you are authenticated (e.g., 'az login') and have the correct AZURE_SUBSCRIPTION_ID.")

view raw JSON →