Python:每分钟只需要请求20次

时间:2015-10-18 18:49:41

标签: python timer request timeout urllib

我已经制作了一个使用api来请求一些数据的python代码,但api只允许每分钟20个请求。我正在使用urllib来请求数据。我也使用for循环,因为数据位于文件中:

for i in hashfile:
    hash = i
    url1 = "https://hashes.org/api.php?act=REQUEST&key="+key+"&hash="+hash
    print(url1)
    response = urllib.request.urlopen(url2).read()
    strr = str(response)

    if "plain" in strr:
        parsed_json = json.loads(response.decode("UTF-8"))
        print(parsed_json['739c5b1cd5681e668f689aa66bcc254c']['plain'])
        writehash = i+parsed_json
        hashfile.write(writehash + "\n")
    elif "INVALID HASH" in strr:
        print("You have entered an invalid hash.")
    elif "NOT FOUND" in strr:
        print("The hash is not found.")
    elif "LIMIT REACHED" in strr:
        print("You have reached the max requests per minute, please try again in one minute.")
    elif "INVALID KEY!" in strr:
        print("You have entered a wrong key!")
    else:
        print("You have entered a wrong input!")

有没有办法让它每分钟做20个请求?或者,如果不可能,我可以在20次尝试后超时吗? (顺便说一句,它只是代码的一部分)

2 个答案:

答案 0 :(得分:4)

您想使用time模块。在每个循环结束时添加time.sleep(3),每分钟最多可获得20个请求

答案 1 :(得分:4)

time.sleep(3)保证您不会每分钟使用您的代码发出超过20个请求,但可能会不必要地延迟允许的请求:想象您只需要发出10个请求:time.sleep(3)请求使循环运行半分钟但是api允许您在这种情况下一次发出所有10个请求(或者至少一个接一个)。

要在不延迟初始请求的情况下强制执行每分钟20个请求的限制,您可以使用RatedSemaphore(20, period=60)

rate_limit = RatedSemaphore(20, 60)
for hash_value in hash_file:
    with rate_limit, urlopen(make_url(hash_value)) as response:
        data = json.load(response)

您甚至可以在遵守费率限制的情况下立即提出多个请求:

from multiprocessing.pool import ThreadPool

def make_request(hash_value, rate_limit=RatedSemaphore(20, 60)):
    with rate_limit:
        try:
            with urlopen(make_url(hash_value)) as response:
                return json.load(response), None
        except Exception as e:
                return None, e


pool = ThreadPool(4) # make 4 concurrent requests
for data, error in pool.imap_unordered(make_request, hash_file):
    if error is None:
        print(data)