在查询VirusTotal时睡觉?

时间:2016-07-25 18:08:21

标签: python wait sleep

我想使用VirusTotal API检查VirusTotal数据库的哈希值,但VirusTotal公共API将请求限制为每分钟4次。我的代码部分将我的哈希值列表(hash_list)与数据库进行比较,如下所示:

url = "https://www.virustotal.com/vtapi/v2/file/report"
parameters = {"resource": hash_list,
              "apikey": "<API KEY HERE>"}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
json_out = response.read()

我需要弄清楚如何在代码中添加一个等待或休眠函数,以便从我的hash_list中检查一个哈希,等待15秒,然后检查另一个哈希,直到列表完成。这将使查询保持每分钟4次,但我无法弄清楚如何添加等待以使其正常工作。

2 个答案:

答案 0 :(得分:0)

import time

/code/

time.sleep(15)

应该有效。只需将time.sleep()代码段添加到块中即可导致延迟。

答案 1 :(得分:-2)

def getdata(hash,apikey):
    params = {'apikey': apikey, 'resource':hash}
    headers = {"Accept-Encoding": "gzip, deflate","User-Agent" : "gzip,  My Python requests library example client or username"}
    response_dict={}
    try:
        r = requests.get('https://www.virustotal.com/vtapi/v2/file/report', params=params)
        if r.status_code == 403:
            return "Forbidden. You don't have enough privileges to make the request"
        elif  r.status_code == 204:
            return "Request rate limit exceeded"
        elif r.status_code == 400:
            return "Bad Request"
        elif r.status_code == 200:
            response_dict = r.json()
            return response_dict
    except Exception as e:
        return "API Request Error"
    return response_dict

for i in range(0,4):
   response_dict=getdata(hash,api_key)
time.sleep(56)

您可以将生成器用于哈希。

请参阅此仓库virustotal_apiclient