This sample demonstrates how to manage your Azure websites using a Python client.
On this page
1. If you don't already have it, install Python.
We recommend to use a virtual environnement to run this example, but it's not mandatory. You can initialize a virtualenv this way:
pip install virtualenv virtualenv mytestenv cd mytestenv source bin/activate
Clone the repository.
git clone https://github.com:Azure-Samples/app-service-web-python-manage.git
Install the dependencies using pip. This step requires
pip
version >=6.0 andsetuptools
version >=8.0. To check that you have the required versions, usepip --version
andeasy_install --version
.cd app-service-web-python-manage pip install -r requirements.txt
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
Set the following environment variables using the information from the service principal that you created.
export AZURE_TENANT_ID={your tenant id} export AZURE_CLIENT_ID={your client id} export AZURE_CLIENT_SECRET={your client secret} export AZURE_SUBSCRIPTION_ID={your subscription id}
[AZURE.NOTE] On Windows, use
set
instead ofexport
.Run the sample.
python example.py
What does example.py do?
The sample creates, lists and updates a website. It starts by setting up a ResourceManagementClient and a WebSiteManagementClient object using your subscription and credentials.
import os
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.web import WebSiteManagementClient
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
credentials = ServicePrincipalCredentials(
client_id=os.environ['AZURE_CLIENT_ID'],
secret=os.environ['AZURE_CLIENT_SECRET'],
tenant=os.environ['AZURE_TENANT_ID']
)
resource_client = ResourceManagementClient(credentials, subscription_id)
web_client = WebSiteManagementClient(credentials, subscription_id)
The sample then sets up a resource group in which it will create the website.
print_item
is a helper function that will print some attributes of the
ResourceGroup
object returned by create_or_update
.
resource_group_params = {'location':'westus'}
print_item(resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params))
Create an App Service plan
Create a service plan to host your webapp.
from azure.mgmt.web.models import AppServicePlan, SkuDescription, Site
service_plan_async_operation = web_client.app_service_plans.create_or_update(
GROUP_NAME,
SERVER_FARM_NAME,
AppServicePlan(
app_service_plan_name=SERVER_FARM_NAME,
location=WEST_US,
sku=SkuDescription(
name='S1',
capacity=1,
tier='Standard'
)
)
)
service_plan = service_plan_async_operation.result()
print_item(service_plan)
Create a website
from azure.mgmt.web.models import Site
site_async_operation = web_client.web_apps.create_or_update(
GROUP_NAME,
SITE_NAME,
Site(
location=WEST_US,
server_farm_id=service_plan.id
)
)
site = site_async_operation.result()
print_item(site)
List websites in the resourcegroup
for site in web_client.web_apps.list_by_resource_group(GROUP_NAME):
print_item(site)
Get details for the given website
web_client.web_apps.get(GROUP_NAME, SITE_NAME)
Delete a website
web_client.web_apps.delete(GROUP_NAME, SITE_NAME)
At this point, the sample also deletes the resource group that it created.
delete_async_operation = resource_client.resource_groups.delete(GROUP_NAME)
delete_async_operation.wait()
More information
Please refer to Azure SDK for Python for more information.