【发布时间】:2018-03-24 04:39:19
【问题描述】:
我编写了一个脚本来从 Qualys 获取扫描结果,每周运行一次以收集指标。
此脚本的第一部分涉及为过去一周运行的每个扫描获取参考列表以进行进一步处理。
问题在于,虽然这有时会完美运行,但有时脚本会挂在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 的可怕组合。请不要对我的评价太苛刻。
标签: python api freeze pycurl qualys