aws lamda with boto3.client从dict和

2024-05-16 22:34:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望你能帮助我 在从aws lambda获得200 ok的响应之后,我要搜索路由中的所有DestinationCidrBlock 但当一张打印出来的时候,我只得到第一张

u'Routes': [{u'Origin': 'CreateRoute', u'DestinationCidrBlock': '1.1.1.1/32', u'NetworkInterfaceId': 'eni-08b854f5bc83cefe4', u'State': 'blackhole'}, {u'Origin': 'CreateRoute', u'DestinationCidrBlock': '2.2.2.2/32', u'NetworkInterfaceId': 'eni-08b854f5bc83cefe4', u'State': 'blackhole'}, {u'GatewayId': 'local', u'DestinationCidrBlock': '172.31.0.0/16', u'State': 'active', u'Origin': 'CreateRouteTable'}, {u'GatewayId': 'igw-cec16ba6', u'DestinationCidrBlock': '0.0.0.0/0', u'State': 'active', u'Origin': 'CreateRoute'}]}]}

如果我这样做,我得到的是值,而不是2.2.2.2/32:

print response["RouteTables"][0]['Routes'][0]['DestinationCidrBlock']

print response["RouteTables"][0]['RouteTableId']

1.1.1.1/32

rtb-08c31263型

但是如果在那里做一个for循环,我就得到这个格式的数字

for x in response["RouteTables"][0]['Routes'][0]['DestinationCidrBlock']:
print x

1个

是的。你知道吗

1个

是的。你知道吗

1个

是的。你知道吗

1个

/

所以我的问题,我希望你能帮助我是如何得到所有的ip地址在那里,并存储在某种类型 在某种dict或list中关联ip和路由表id,以获得格式良好的所有数据


Tags: forresponse格式originactivestateprintroutes
1条回答
网友
1楼 · 发布于 2024-05-16 22:34:59

["RouteTables"][0]保存结果返回的第一个路由表,如果只有一个路由表,则可以这样做,但如果有更多路由表,并且需要考虑其中的每一个,则还需要遍历["RouteTables"]的内容。你知道吗

我假设您只有一个route表。你知道吗

您已经从中获得路由表id

["RouteTables"][0]["RouteTableId"]

如果要提取每个目标cidr块,则需要循环遍历

["RouteTables"][0]["Routes"]

循环时,提取那些苹果酒块,即

cirdBlocks = [route["DestinationCidrBlock"] for route in response["RouteTables"][0]["Routes"]]

要创建键为route table ID、值为上述cidr块的字典,只需执行以下操作

routeTableId = response["RouteTables"][0]["RouteTableId"]
cirdBlocks = [route["DestinationCidrBlock"] for route in response["RouteTables"][0]["Routes"]]
routeTableCidrAssoc = { routeTableId: cirdBlocks }

相关问题 更多 >