我编写了一个脚本来从 Qualys 获取扫描结果,每周运行一次以收集指标。
该脚本的第一部分涉及为过去一周运行的每个扫描获取参考列表以进行进一步处理。
问题是,虽然有时这会很好地工作,但有时脚本会挂起c.perform()
。这在手动运行脚本时是可以管理的,因为它可以重新运行直到它工作。但是,我希望每周将其作为计划任务运行,而无需任何手动交互。
是否有一种万无一失的方法可以检测是否发生挂起并重新发送 PyCurl 请求直到它起作用?
我尝试设置c.TIMEOUT
andc.CONNECTTIMEOUT
选项,但这些似乎没有效果。此外,由于没有抛出异常,简单地将它放在 try-except 块中也不会飞。
有问题的功能如下:
# Retrieve a list of all scans conducted in the past week
# Save this to refs_raw.txt
def getScanRefs(usr, pwd):
print("getting scan references...")
with open('refs_raw.txt','wb') as refsraw:
today = DT.date.today()
week_ago = today - DT.timedelta(days=7)
strtoday = str(today)
strweek_ago = str(week_ago)
c = pycurl.Curl()
c.setopt(c.URL, 'https://qualysapi.qualys.eu/api/2.0/fo/scan/?action=list&launched_after_datetime=' + strweek_ago + '&launched_before_datetime=' + strtoday)
c.setopt(c.HTTPHEADER, ['X-Requested-With: pycurl', 'Content-Type: text/xml'])
c.setopt(c.USERPWD, usr + ':' + pwd)
c.setopt(c.POST, 1)
c.setopt(c.PROXY, 'companyproxy.net:8080')
c.setopt(c.CAINFO, certifi.where())
c.setopt(c.SSL_VERIFYPEER, 0)
c.setopt(c.SSL_VERIFYHOST, 0)
c.setopt(c.CONNECTTIMEOUT, 3)
c.setopt(c.TIMEOUT, 3)
refsbuffer = BytesIO()
c.setopt(c.WRITEDATA, refsbuffer)
c.perform()
body = refsbuffer.getvalue()
refsraw.write(body)
c.close()
print("Got em!")