Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查询VirusTotal时睡觉?_Python_Wait_Sleep - Fatal编程技术网

Python 查询VirusTotal时睡觉?

Python 查询VirusTotal时睡觉?,python,wait,sleep,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 = url

我想使用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()
我需要弄清楚如何在代码中添加wait或sleep函数,以便它从我的hash_列表中检查一个hash,等待15秒,然后检查另一个hash,直到列表完成。这将使查询保持在每分钟4次,但我不知道如何添加等待以使其正常工作

import time

/code/

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

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)
您可以使用生成器进行散列


参考此repo

我是否必须以某种方式将其添加到循环中,以便将每个结果添加到json_out变量中?或者我应该把它变成一个函数,然后直接调用它?@P.J.两种方法都可以,真的。将其放入循环中可能是最简单的方法。