从 JSON 文件接收 IP

Receive IPs from JSON file

我正在尝试使用 Python 2.7.5 从 JSON 文件中获取所有 IP 但是我无法正确地做到这一点。 有人建议我如何在 txt 文件中接收来自 ('addressPrefixes') 的所有 IP 吗?

这是我下载 json 文件的代码:

import urllib
import json
from urllib import urlopen


testfile = urllib.URLopener()
testfile.retrieve("https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13- 
DA13A5DE5B63/ServiceTags_Public_20210426.json", "AzureIPs.json")

print("---SUCCESSFULLY RECEIVED MICROSOFT AZURE IPS---")

with open('AzureIPs.json','r') as f:
     data = json.load(f)

JSON 文件包含许多 IP 和 IP 范围,如下所示:

 {
 "changeNumber": 145,
 "cloud": "Public",
 "values": [
 {
    "name": "ActionGroup",
    "id": "ActionGroup",
    "properties": {
       "changeNumber": 9,
       "region": "",
       "regionId": 0,
       "platform": "Azure",
       "systemService": "ActionGroup",
       "addressPrefixes": [
          "13.66.60.119/32",
          "13.66.143.220/30",
          "13.66.202.14/32",
          "13.66.248.225/32",
          "13.66.249.211/32",
          "13.67.10.124/30",
          "13.69.109.132/30",
          "13.71.199.112/30",
          "13.77.53.216/30",
          "13.77.172.102/32",
          "13.77.183.209/32",
          "13.78.109.156/30",
          "13.84.49.247/32",         
          "2603:1030:c06:400::978/125",
          "2603:1030:f05:402::178/125",
          "2603:1030:1005:402::178/125",
          "2603:1040:5:402::178/125",
          "2603:1040:207:402::178/125",
          "2603:1040:407:402::178/125",
          "2603:1040:606:402::178/125",
          "2603:1040:806:402::178/125",
          "2603:1040:904:402::178/125",
          "2603:1040:a06:402::178/125",
          "2603:1040:b04:402::178/125",
          "2603:1040:c06:402::178/125",
          "2603:1040:d04:800::f8/125",
          "2603:1040:f05:402::178/125",
          "2603:1040:1104:400::178/125",
          "2603:1050:6:402::178/125",
          "2603:1050:403:400::1f8/125"
      ],
       "networkFeatures": [
        "API",
        "NSG",
        "UDR",
        "FW"
      ]
    }
  },
  {
  "name": "ApplicationInsightsAvailability",
  "id": "ApplicationInsightsAvailability",
  "properties": {
    "changeNumber": 2,
    "region": "",
    "regionId": 0,
    "platform": "Azure",
    "systemService": "ApplicationInsightsAvailability",
    "addressPrefixes": [
      "13.86.97.224/27",
      "13.86.98.0/27",
      "13.86.98.48/28",
      "13.86.98.64/28",
      "20.37.156.64/27",
      "20.37.192.80/29",
      "20.38.80.80/28",
      "20.40.104.96/27",
      "20.40.104.128/27",
      "20.40.124.176/28",
      "20.40.124.240/28",
      "20.40.125.80/28",
      "20.40.129.32/27",
      "20.40.129.64/26",
      "20.40.129.128/27",
      "20.42.4.64/27",
      "20.42.35.32/28",
      "20.42.35.64/26",
      "20.42.35.128/28",
      "20.42.129.32/27",
      "20.43.40.80/28",
      "20.43.64.80/29",
      "20.43.128.96/29",
      "20.45.5.160/27",
      "20.45.5.192/26",
      "20.189.106.64/29",
      "23.100.224.16/28",
      "23.100.224.32/27",
      "23.100.224.64/26"
    ],
    "networkFeatures": [
      "API",
      "NSG",
      "UDR",
      "FW"
    ]
  }
},
{
  "name": "AzureActiveDirectory",
  "id": "AzureActiveDirectory",
  "properties": {
    "changeNumber": 8,
    "region": "",
    "regionId": 0,
    "platform": "Azure",
    "systemService": "AzureAD",
    "addressPrefixes": [
      "13.64.151.161/32",
      "13.66.141.64/27",
      "13.67.9.224/27",
      "13.69.66.160/27",
      "13.69.229.96/27",
      "13.70.73.32/27"
    ],
    "networkFeatures": [
      "API",
      "NSG",
      "UDR",
      "FW",
      "VSE"
    ]
  }
}

感谢您的宝贵时间。

import urllib
import json
from urllib import urlopen


testfile = urllib.URLopener()
testfile.retrieve("https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13- 
DA13A5DE5B63/ServiceTags_Public_20210426.json", "AzureIPs.json")

print("---SUCCESSFULLY RECEIVED MICROSOFT AZURE IPS---")

with open('AzureIPs.json','r') as f:
     data = json.load(f)


#################  CHANGES AFTER THIS LINE  ################# 

    ips = []

    values = data['values']
    for block in values:
        ips.append(block.properties.addressPrefixes)

但是你会得到 2D array 使用这种方法,如果你需要 1D array 而不是将 IP 块与 values 中每个相应的块分开,你可以使用下面的代码来展平数组。

import numpy as np

2DArray = np.array(ips)
1DArray = 2DArray.flatten()