首页 > 解决方案 > 在发布 API 之前休眠 X 秒 - Python

问题描述

我需要在网站上发布一些记录。我觉得我已经完成了复杂的部分 - 代码本身,现在我需要调整代码,以便我的帐户在发布时不会被阻止 - 是的,只是发生了。

#importing libraries
import csv
import json

#changing data type
field_types = [('subject', str),
('description', str),
('email', str)]
output = []

#opening the raw file
with open('file.csv','r',encoding = 'utf-8-sig') as f:
    for row in csv.DictReader(f):
        row.update((key, conversion(row[key]))
        for key, conversion in field_types)
        output.append(row) #appending rows
    with open('tickets.json','w') as outfile: #saving records as json
        json.dump(output,outfile,sort_keys = True, indent = 4)
with open('tickets.json','r')as infile:
    indata = json.load(infile)
output =[]
for data in indata:
    r= requests.post("https://"+ domain +".domain.com/api/", auth = (api_key, password), headers = headers, json=data)
    output.append(json.loads(r.text))
#saving the response code
with open('response.json', 'w') as outfile:
    json.dump(output, outfile, indent = 4)

我搜索并找到了 time.sleep(5) 但现在知道如何使用它了。会先过去output.append(json.loads(r.text))吗?

标签: python

解决方案


推荐阅读