概述
我有一个
python
脚本检查我的Vuln扫描仪正在扫描哪些实例。API响应位于
XML
我用的是
lxml
图书馆
etree
循环响应并分析每个响应
标签。但是,我的循环只返回一个实例ID。下面是我的代码
XML
以及脚本的输出。
CODE:
import os
import requests
import boto3
import lxml
from lxml import etree
s3 = boto3.resource('s3')
def apiLogin():
global s
s = requests.Session()
qualys_username = "xxxxxx"
qualys_password = "xxxxxx"
payload = {'action':'login', 'username':qualys_username,
'password':qualys_password}
s.headers.update({'X-Requested-With':qualys_username})
r = s.post('https://qualysapi.qualys.com/api/2.0/fo/session/',
data=payload)
def launchReport():
payload = {'action':'list', 'use_tags':'1', 'tag_set_include':'xxxxxx', 'host_metadata':'ec2', 'host_metadata_fields':'instanceId'}
r = s.post('https://qualysapi.qualys.com/api/2.0/fo/asset/host/', data=payload)
os.chdir('/tmp')
f = open('qualys_instances.xml','w')
print(r.text, file=f)
file = open('qualys_instances.xml','rb')
s3.Bucket('xxxxxx').put_object(Key='qualys_instances.xml', Body=file)
def formatReport():
bucket = 'xxxxxx'
key = 'xxxxxx'
os.chdir('/tmp')
obj = s3.Object(bucket, key)
body = obj.get()['Body'].read()
doc = etree.fromstring(body)
host_list = doc.xpath('//EC2_INSTANCE_ID')
for i in host_list:
print(i)
k = open('qualys_instances.txt','w')
print(i.text, file=k)
file = open('qualys_instances.txt','rb')
s3.Bucket('nwm-all-instances').put_object(Key='qualys_instances.txt', Body=file)
XML:
2018-05-21T21:38:06Z
xxxxxx
xxxxxx
EC2
Fail
2018-05-16T03:41:14Z
.
xxxxxx
xxxxxx
EC2
Fail
2018-05-16T03:27:35Z
.
SCRIPT OUTPUT:
i-111111
最后
以上就是听话身影为你收集整理的python for循环迭代_python3-for循环只返回一个迭代的全部内容,希望文章能够帮你解决python for循环迭代_python3-for循环只返回一个迭代所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复