【Azure 应用服务】使用Python Azure SDK 来获取 App Service的访问限制信息(Access Restrictions)

简介: 【Azure 应用服务】使用Python Azure SDK 来获取 App Service的访问限制信息(Access Restrictions)

问题描述

为Azure App Service添加访问限制,需要Python Azure SDK来实现的示例代码。

问题解答

查阅Azure App Service的官方资料,使用Python SDK有 azure-mgmt-web 包中的 WebSiteManagementClient 类可以对Azure App Service资源进行管理。

Access Restrictions属于App Service的配置项,所以可以通过 client类中的 web_apps.get_configuration 获取,及通过 web_apps.create_or_update_configuration 进行创建或修改。

get_configuration 方法的返回数据类型为 SiteConfig,其中包含了 ip_security_restrictions 属性值。而且它是一个List类型,其数据结构类型为 IpSecurityRestriction

 

综上所述,通过Python SDK获取App Service的Access Restrictions的示例代码如下:

import os
from azure.identity import ClientSecretCredential, AzureAuthorityHosts
from msrestazure.azure_cloud import AZURE_CHINA_CLOUD
from azure.mgmt.web import WebSiteManagementClient
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
myapp_base_url = "https://management.chinacloudapi.cn"
credentials = ClientSecretCredential(
    client_id=os.environ['AZURE_CLIENT_ID'],
    client_secret=os.environ['AZURE_CLIENT_SECRET'],
    tenant_id=os.environ['AZURE_TENANT_ID'],
    authority=AzureAuthorityHosts.AZURE_CHINA
)
def print_item(group):
    """Print some properties of an Azure model."""
    print("\tName: {}".format(group.name))
    print("\tId: {}".format(group.id))
    print("\tLocation: {}".format(group.location))
    print("\tTags: {}".format(group.tags))
    if hasattr(group, 'status'):
        print("\tStatus: {}".format(group.status))
    if hasattr(group, 'state'):  # Site
        print("\tStatus: {}".format(group.state))
    if hasattr(group, 'properties'):
        print_properties(group.properties)
    print("\n\n")
def print_properties(props):
    """Print some properties of a Site."""
    if props and props.provisioning_state:
        print("\tProperties:")
        print("\t\tProvisioning State: {}".format(props.provisioning_state))
def print_ipsecurityrestrictions(iprestrictions):
    """Print ip security restrictions of a Site."""
    for restrits in iprestrictions:
        print("\t for rule : {}".format(restrits.name))
        print("\t\t name : {}".format(restrits.name))
        print("\t\t ip_address : {}".format(restrits.ip_address))
        print("\t\t subnet_mask : {}".format(restrits.subnet_mask))
        print("\t\t action : {}".format(restrits.action))
        print("\t\t vnet_subnet_resource_id : {}".format(restrits.vnet_subnet_resource_id))
        print("\t\t vnet_traffic_tag : {}".format(restrits.vnet_traffic_tag))
        print("\t\t subnet_traffic_tag : {}".format(restrits.subnet_traffic_tag))
        print("\t\t tag : {}".format(restrits.tag))
        print("\t\t priority : {}".format(restrits.priority))
        print("\t\t description : {}".format(restrits.description))
        print("\n\n")
web_client = WebSiteManagementClient(credentials, subscription_id, base_url=myapp_base_url, credential_scopes=[
                                      AZURE_CHINA_CLOUD.endpoints.management + "/.default"])
resource_group_name = "app-rg"
for site in web_client.web_apps.list_by_resource_group(resource_group_name):
    print_item(site)
print("\n\n")
print("\nStart to get web app configs")
site_name = "testwebapp04"
configs = web_client.web_apps.get_configuration(resource_group_name, site_name)
print_ipsecurityrestrictions(configs.ip_security_restrictions)
#web_client.web_apps.create_or_update_configuration()

执行结果为:

 

 

附录一: 调试Ptyhon代码时,遇上了奇怪的AAD错误 azure.core.exceptions.ClientAuthenticationError: Authentication failed: AADSTS70011: The provided request must include a 'scope' input parameter. The provided value for the input parameter 'scope' is not valid. The scope . . . / / / : a a a a a c c c d d e e e f g h h i i l l m m n n n n o p p s t t t t u u is not valid.

最后通过换一台执行代码的虚拟机后,同样的代码没有报错。所以最终定位到是本机环境中,给中azure identity 的包不一致导致。通过 pip -m list出两个环境不一样的module版本后,再本地重新安装azure identity 和 azure core 两个包后,问题消失!

azure-identity == 1.11.0
azure-core == 1.24.2 #1.25.1

 

参考资料

Manage Azure websites with Python : https://github.com/Azure-Samples/app-service-web-python-manage

 

相关文章
|
5月前
|
JSON 监控 数据格式
1688 item_search_app 关键字搜索商品接口深度分析及 Python 实现
1688开放平台item_search_app接口专为移动端优化,支持关键词搜索、多维度筛选与排序,可获取商品详情及供应商信息,适用于货源采集、价格监控与竞品分析,助力采购决策。
|
5月前
|
缓存 监控 Android开发
京东 item_get_app 接口深度分析及 Python 实现
京东item_get_app接口可获取商品原始详情数据,包含更丰富的字段和细节,适用于电商分析、价格追踪等场景。需通过认证获取权限,支持字段筛选和区域化数据查询。
|
6月前
|
缓存 数据挖掘 API
淘宝 item_get_app 接口深度分析及 Python 实现
淘宝item_get_app接口是淘宝开放平台提供的移动端商品详情数据获取接口,相较PC端更贴近APP展示效果,支持获取APP专属价格、促销活动及详情页结构,适用于电商导购、比价工具、数据分析等场景。接口采用appkey+appsecret+session认证机制,需申请相应权限。本文提供Python调用示例及使用注意事项,帮助开发者高效对接移动端商品数据。
|
4月前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
580 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
4月前
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
504 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
4月前
|
移动开发 Rust JavaScript
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
896 4
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
9月前
|
人工智能 文件存储 数据中心
Ollama部署本地大模型并通过Infortress APP远程访问保姆级教程
本文介绍如何快速上手本地大模型部署工具Ollama及AI远程访问工具Infortress。通过Ollama,开发者可轻松部署如Llama、Deepseek等主流开源模型,仅需几行命令即可完成安装与运行。结合Infortress,用户能实现对本地大模型的远程访问,支持多设备无缝对接,同时提供便捷的模型切换与知识库管理功能。Infortress更兼具NAS软件特性,成为个人AI数据中心的理想选择。
|
10月前
|
存储 安全 数据安全/隐私保护
【Azure Function App】在Function App中使用System Managed Identity访问Storage Account
本文介绍了如何在Azure Function中使用托管身份(Managed Identity)替代AzureWebJobsStorage连接函数应用到存储账户,以提高安全性并减少Access Key的使用。具体步骤包括:1) 启用系统分配的身份;2) 为函数应用授予存储访问权限,添加必要角色(如Storage Blob Data Contributor);3) 配置`AzureWebJobsStorage__blobServiceUri`参数指定Blob Service Uri。完成后删除旧配置,即可通过Managed Identity访问Storage Account。
331 19
|
10月前
aliyun-upload-sdk访问空值Bug
这段内容描述了一个代码问题:方法调用时未正确处理参数。第一个图显示方法需要传入参数,但未进行空值判断;第二个图显示调用时未传参,导致报错并使上传失败。这是典型的参数处理不当引发的运行时错误,需完善判空逻辑和参数传递以解决问题。
287 1
aliyun-upload-sdk访问空值Bug
|
4月前
|
移动开发 Android开发
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
258 0

推荐镜像

更多