我有一个脚本,该脚本通过多次调用API来生成CSV,该脚本过去已成功运行,但是现在我收到了Errno消息。最常见的是[Errno 54]由对等方重置连接,[Errno 60]操作超时,有时甚至提供了[Errno 8]节点名或服务名,或者未知。
这个特定的脚本调用API大约需要16-24k次(我100%确信这低于速率限制),并且成功运行后大约需要2-3个小时才能完成。由于某种原因,我似乎无法使其成功运行,而不会在脚本运行中出现一段时间的错误。因此,它将调用API路由数百/数千次,然后无处不吐出错误之一,以调用已经成功调用的相同错误。好像很奇怪我还提供了以下错误消息。
我已删除一些敏感信息,例如令牌和API路由,因为这是私有数据!不过我还是保留了这些错误。
import http.client
import json
import csv
class mgrData:
def __init__(self):
self.token = '[REMOVED]'
self.userDict = {}
def getPeakData(self, route):
conn = http.client.HTTPSConnection("[REMOVED]")
bod = ''
headers = {
'Authorization': 'Bearer '+ str(self.token),
'Content-Type': 'application/json'
}
conn.request("GET", route, bod, headers)
res = conn.getresponse()
data = res.read()
return json.loads(data)
def PeakExport(self):
print("Please wait, data generating...")
raw = self.getPeakData("[REMOVED]")
totalrecords = raw['meta']['page']['total']
resultspp = len(raw['data'])
if totalrecords % resultspp == 0:
numpages = int(totalrecords/resultspp)
elif totalrecords % resultspp != 0:
numpages = int(totalrecords//resultspp + 1)
mainDct = {}
for i in range(1,numpages+1):
route = "[REMOVED]"
dct = self.getPeakData(route)
data = dct['data']
for i in data:
pkEmpID = i['attributes']['identifier'].split('_')[0]
name = i['attributes']['name']
# segID = i['id']
hrisIDget = self.getPeakData("[REMOVED]")
hrisID = hrisIDget['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User']['employeeNumber']
if pkEmpID in mainDct:
if i['attributes']['direct'] == True:
dSegID = i['id']
mainDct[pkEmpID].append({'dSegID' : dSegID})
if i['attributes']['direct'] == False:
aSegID = i['id']
mainDct[pkEmpID].append({'aSegID' : aSegID})
else:
mainDct.update({pkEmpID : []})
mainDct[pkEmpID].append({'name': name})
mainDct[pkEmpID].append({'EID' : hrisID})
if i['attributes']['direct'] == True:
dSegID = i['id']
mainDct[pkEmpID].append({'dSegID' : dSegID})
if i['attributes']['direct'] == False:
aSegID = i['id']
mainDct[pkEmpID].append({'aSegID' : aSegID})
urllist = []
for key in mainDct:
switch = False
for i in mainDct[key]:
if 'aSegID' in i:
switch = True
data = self.getPeakData("[REMOVED]" % (i['aSegID']))
if 'mean' in data['data']['attributes']['scores']:
score = data['data']['attributes']['scores']['mean']
participation = data['data']['attributes']['participation']['mean']
nps = data['data']['attributes']['scores']['nps']['score']
mainDct[key].append({'engagement all': score})
mainDct[key].append({'nps all': nps})
mainDct[key].append({'participation all' : participation})
elif 'anonymity' in data['data']['attributes']['scores']:
mainDct[key].append({'engagement all':'Anonymity'})
mainDct[key].append({'nps all': 'Anonymity'})
mainDct[key].append({'participation all': 'Anonymity'})
else:
mainDct[key].append({'engagement all': 'N/A'})
mainDct[key].append({'nps all':'N/A'})
mainDct[key].append({'participation all': 'N/A'})
if switch == False:
mainDct[key].append({'aSegID' : 'None'})
mainDct[key].append({'engagement all' :'N/A'})
mainDct[key].append({'nps all': 'N/A'})
mainDct[key].append({'participation all: N/A'})
for key in mainDct:
switch = False
for i in mainDct[key]:
if 'dSegID' in i:
switch = True
data = self.getPeakData("[REMOVED]" % (i['dSegID']))
if 'mean' in data['data']['attributes']['scores']:
score = data['data']['attributes']['scores']['mean']
participation = data['data']['attributes']['participation']['mean']
nps = data['data']['attributes']['scores']['nps']['score']
mainDct[key].append({'engagement direct': score})
mainDct[key].append({'nps direct': nps})
mainDct[key].append({'participation direct': participation})
elif 'anonymity' in data['data']['attributes']['scores']:
mainDct[key].append({'engagement direct': 'Anonymity'})
mainDct[key].append({'nps direct': 'Anonymity'})
mainDct[key].append({'participation direct': 'Anonymity'})
else:
mainDct[key].append({'engagement direct': 'N/A'})
mainDct[key].append({'nps direct': 'N/A'})
mainDct[key].append({'participation direct': 'N/A'})
if switch == False:
mainDct[key].append({'dSegID' : 'None'})
mainDct[key].append({'engagement direct': 'N/A'})
mainDct[key].append({'nps direct': 'N/A'})
mainDct[key].append({'participation direct': 'N/A'})
with open('Output.csv', 'w') as csv_file:
csvwriter = csv.writer(csv_file, delimiter=',')
csvwriter.writerow(['Manager ID', 'Manager Name','Direct Engagement', 'All Engagement', 'Direct NPS', 'All NPS', 'Direct Participation', 'All Participation'])
for key in mainDct:
for i in mainDct[key]:
if 'name' in i:
name = i['name']
if 'EID' in i:
EID = i['EID']
if 'nps direct' in i:
ND = i['nps direct']
if 'engagement direct' in i:
ED = i['engagement direct']
if 'participation direct' in i:
PD = i['participation direct']
if 'engagement all' in i:
EA = i['engagement all']
if 'nps all' in i:
NA = i['nps all']
if 'participation all' in i:
PA = i['participation all']
csvwriter.writerow([EID,name,ED,EA,ND,NA,PD,PA])
def main():
thisSync = mgrData()
thisSync.PeakExport()
main()
我收到一些错误:
Please wait, data generating...
Traceback (most recent call last):
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 209, in <module>
main()
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 204, in main
thisSync.PeakExport()
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 72, in PeakExport
hrisIDget = self.getPeakData("/scim/v2/users/employee_"+str(pkEmpID))
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 31, in getPeakData
res = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1332, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 54] Connection reset by peer
[Finished in 1263.8s with exit code 1]
[cmd: ['/Library/Frameworks/Python.framework/Versions/3.8/bin/python3', '-u', '/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py']]
[dir: /Volumes/GoogleDrive/My Drive/BI Scripts]
[path: /Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
Please wait, data generating...
Traceback (most recent call last):
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 209, in <module>
main()
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 204, in main
thisSync.PeakExport()
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 72, in PeakExport
hrisIDget = self.getPeakData("/scim/v2/users/employee_"+str(pkEmpID))
File "/Volumes/GoogleDrive/My Drive/BI Scripts/Random.py", line 31, in getPeakData
res = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1332, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
TimeoutError: [Errno 60] Operation timed out