0
我已经编写了一个脚本来获取Qualys的扫描结果,以便每周进行度量收集。PyCurl请求在执行时无限挂起
该脚本的第一部分涉及获取上一周运行的每个扫描的引用列表以供进一步处理。
问题是,虽然这有时会很好地工作,但其他时间脚本将挂在c.perform()
行。当手动运行脚本时这是可管理的,因为它可以重新运行直到它工作。不过,我希望每周都将此作为计划任务运行,而无需任何手动交互。
有没有一种万无一失的方法,我可以检测到是否发生挂起并重新发送PyCurl请求,直到它工作?
我试过设置c.TIMEOUT
和c.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!")
我现在意识到变量命名的时候,我已经使用驼峰,under_scores的可怕的组合,和nothingatall。请不要太苛刻地评价我。 –