適用於 Python 的 Azure 網路程式庫

概觀

Azure 虛擬網路可讓您連線 Azure 資源,還能將它們連線至內部部署網路。

若要開始使用 Azure 虛擬網路,請參閱建立您的第一個虛擬網路

管理 API

使用管理 API 來檢查、管理及設定 Azure 虛擬網路。

不同於其他 Azure Python API,網路 API 會明確地建立版本以分成個別套件。 您不需要將它們個別匯入,因為套件資訊會在用戶端建構函式中加以指定。

使用 pip 安裝管理套件。

pip install azure-mgmt-network

範例

建立虛擬網路和相關聯的子網路。

from azure.mgmt.network import NetworkManagementClient

GROUP_NAME = 'resource-group'
VNET_NAME = 'your-vnet-identifier'
LOCATION = 'region'
SUBNET_NAME = 'your-subnet-identifier'

network_client = NetworkManagementClient(credentials, 'your-subscription-id')

async_vnet_creation = network_client.virtual_networks.create_or_update(
    GROUP_NAME,
    VNET_NAME,
    {
        'location': LOCATION,
        'address_space': {
            'address_prefixes': ['10.0.0.0/16']
        }
    }
)
async_vnet_creation.wait()

# Create Subnet
async_subnet_creation = network_client.subnets.create_or_update(
    GROUP_NAME,
    VNET_NAME,
    SUBNET_NAME,
    {'address_prefix': '10.0.0.0/24'}
)
subnet_info = async_subnet_creation.result()

範例

檢視 Azure 虛擬網路範例的完整清單