问题描述

为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

 

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

  1. 【Azure 应用服务】Python flask 应用部署在Aure App Service 遇见的 3 个问题

    在App Service(Windows)中部署Flask应用时的注意事项: ● 添加Python扩展插件,Python 3.6.4 x64: ●● 配置 FastCGI 处理程序,添加Web.con ...

  2. 【Azure 应用服务】Python flask 应用部署在Aure App Service中作为一个子项目时,解决遇见的404 Not Found问题

    问题描述 在成功的部署Python flask应用到App Service (Windows)后,如果需要把当前项目(如:hiflask)作为一个子项目(子站点),把web.config文件从wwwr ...

  3. 【Azure 应用服务】由 Azure Functions runtime is unreachable 的错误消息推导出 ASYNC(异步)和 SYNC(同步)混用而引起ThreadPool耗尽问题

    问题描述 在Azure Function Portal上显示: Azure Functions runtime is unreachable,引起的结果是Function App目前不工作,但是此前一 ...

  4. 【Azure 应用程序见解】 Application Insights 对App Service的支持问题

    问题描述 Web App 发布后, Application Insights 收集不到数据了 问题分析 在应用服务(App Service)中收集应用的监控数据(如Request,Exception, ...

  5. 获取APP应用的包名信息

    语言: python 3.7 需求:获取APP的包名和程序入口信息,以便在 Appium 脚本中配置 appPackage 和 appActivity 参数. 场景一 资源:已有APP应用的apk安装 ...

  6. Appium Python 四:怎样获取APP的Package以及Activity

    看到一篇很好的博客:[Android测试][随笔]获得App的包名和启动页Activity 除了博客上的方法,我还找到两种方法: 方法一:aapt 前提需要使用SDK Manager.exe 下载 A ...

  7. 【Azure 应用服务】App Service For Container 配置Nginx,设置/home/site/wwwroot/目录为启动目录,并配置反向代理

    问题描述 通过Docker Desktop for Linux,配置Nginx镜像后,自定义nginx.conf文件,修改启动目录和对 /out 路径的反向代理到博客园的博文地址 (https://w ...

  8. 【Azure 应用服务】App Service与APIM同时集成到同一个虚拟网络后,如何通过内网访问内部VNET的APIM呢?

    问题描述 App Service访问的APIM已配置内部虚拟网络(Internal VNet)并拥有内网IP地址.App Service与APIM都在相同的虚拟网络(VNET)中.App Servic ...

  9. 【Azure 应用服务】App Service 在使用GIt本地部署,上传代码的路径为/home/site/repository,而不是站点的根目录/home/site/wwwroot。 这个是因为什么?

    问题描述 App Service 在使用GIt本地部署,上传代码的路径为/home/site/repository,而不是站点的根目录/home/site/wwwroot. 这个是因为什么? 并且通过 ...

  10. 【Azure 应用服务】备份网站时由于文件太大了,导致应用服务备份失败。如何解决?

    问题描述 备份网站时由于文件太大了,导致应用服务备份失败.如何解决呢? 问题分析 App Service (应用服务)的备份功能有10GB大小的限制,超过了是无法备份成功的并且该限制是无法扩大的.查看 ...

随机推荐

  1. Linux 安装宋体字体的简单办法

    1. 今天同事说测试环境(CentOS) 打印有异常,无法将汉字正常打印出来. 2. 开发同事提供的思路是安装上宋体的字体再进行尝试,并且给出了一个解决方案的地址: https://blog.csdn ...

  2. STM32CubeMX教程25 PWR 电源管理 - 睡眠、停止和待机模式

    1.准备材料 开发板(正点原子stm32f407探索者开发板V2.4) STM32CubeMX软件(Version 6.10.0) 野火DAP仿真器 keil µVision5 IDE(MDK-Arm ...

  3. Spring缓存是如何实现的?如何扩展使其支持过期删除功能?

    前言:在我们的应用中,有一些数据是通过rpc获取的远端数据,该数据不会经常变化,允许客户端在本地缓存一定时间. 该场景逻辑简单,缓存数据较小,不需要持久化,所以不希望引入其他第三方缓存工具加重应用负担 ...

  4. 语言模型的预训练[6]:思维链(Chain-of-thought,CoT)定义原理详解、Zero-shot CoT、Few-shot CoT 以及在LLM上应用

    大语言模型的预训练[6]:思维链(Chain-of-thought,CoT)定义原理详解.Zero-shot CoT.Few-shot CoT 以及在LLM上应用 1.思维链定义 背景 在 2017- ...

  5. Python 提取图片中的GPS信息

    JPG图片中默认存在敏感数据,例如位置,相机类型等,可以使用Python脚本提取出来,加以利用,自己手动拍摄一张照片,然后就能解析出这些敏感数据了,对于渗透测试信息搜索有一定帮助,但有些相机默认会抹除 ...

  6. scrapy抓取校花网图片

    一:基础版(抓取首页图片) 爬虫py文件代码: 1 # -*- coding: utf-8 -*- 2 import scrapy 3 import sys 4 import io 5 from sc ...

  7. 影驰RTX 4070 SUPER星曜OC显卡评测:250W超频潜力十足 散热更惊喜

    一.前言:影驰推出主打高颜值的RTX 4070 SUPER星曜OC显卡 影驰作为DIY大厂,要说它家颜值最高的产品,那必然就是星曜系列,无论显卡.内存还是SSD,不光好看,品质和性能上也都有着不俗的表 ...

  8. yapi 启动后,老是自动关闭的问题。

    解决方法只需要2步: 1.在命令后面加 & 符号,放到后台执行,最终的命令为: node /usr/local/yapi/yapi/vendors/server/app.js & 2. ...

  9. DNS正向解析

    实验介绍:正向解析 通常把域名到IP称为正向解析 把ip到域名称为反向解析 一:前期准备 准备一台客户端测试正向解析是否正常 修改ip 子网掩码 DNS服务器 使用VMnet8 IP要和DNS服务器端 ...

  10. Pandas处理股票数据

    import tushare as ts import pandas as pd # 下载茅台所有股票交易数据 # df = ts.get_k_data(code="600519" ...