Questions? Feedback? powered by Olark live chat software
Navigáció kihagyása

Manage Azure websites with Python

Utolsó frissítés: 2016.06.20.
Közreműködők:
Szerkesztés a GitHubon

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.

  1. 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
    
  2. Clone the repository.

    git clone https://github.com:Azure-Samples/app-service-web-python-manage.git
    
  3. Install the dependencies using pip.

    cd app-service-web-python-manage
    pip install -r requirements.txt
    
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Set the following environment variables using the information from the service principle 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 of export.

  6. 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.get(
    'AZURE_SUBSCRIPTION_ID',
    '11111111-1111-1111-1111-111111111111') # your 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.

resource_group_params = {'location':'westus'}

Create a server farm

Create a server farm to host your website.

from azure.mgmt.web.models import ServerFarmWithRichSku, SkuDescription

server_farm_async_operation = web_client.server_farms.create_or_update_server_farm(
    GROUP_NAME,
    SERVER_FARM_NAME,
    ServerFarmWithRichSku(
        location=WEST_US,
        sku=SkuDescription(
            name='S1',
            capacity=1,
            tier='Standard'
        )
    )
)
server_farm = server_farm_async_operation.result()
print_item(server_farm)

Create a website

from azure.mgmt.web.models import Site

site_async_operation = web_client.sites.create_or_update_site(
    GROUP_NAME,
    SITE_NAME,
    Site(
        location=WEST_US,
        server_farm_id=server_farm.id
    )
)
site = site_async_operation.result()
print_item(site)

List websites in the resourcegroup

for site in web_client.sites.get_sites(GROUP_NAME).value:
    print_item(site)

Get details for the given website

web_client.sites.get_site(GROUP_NAME, SITE_NAME)

Delete a website

web_client.sites.delete_site(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.