我每隔90秒使用pygsheets python模块从google中获取信息。
在清晨(通常在凌晨2点至3点之间),此操作失败,我会记录此错误:
Traceback (most recent call last):
File "/etc/naemon/naemon-automation/exec/pull-GSheets-CSV.py", line 42, in <module>
wks.export(pygsheets.ExportType.CSV, path=outputDir + '/', filename=outputFileName)
File "/usr/local/lib/python3.5/dist-packages/pygsheets/worksheet.py", line 1306, in export
self.client.drive.export(self, file_format=file_format, filename=filename, path=path)
File "/usr/local/lib/python3.5/dist-packages/pygsheets/drive.py", line 210, in export
status, done = downloader.next_chunk()
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/http.py", line 686, in next_chunk
'GET', headers=headers)
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/http.py", line 183, in _retry_request
raise exception
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/http.py", line 164, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/google_auth_httplib2.py", line 198, in request
uri, method, body=body, headers=request_headers, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/httplib2/__init__.py", line 1926, in request
cachekey,
File "/usr/local/lib/python3.5/dist-packages/httplib2/__init__.py", line 1595, in _request
conn, request_uri, method, body, headers
File "/usr/local/lib/python3.5/dist-packages/httplib2/__init__.py", line 1501, in _conn_request
conn.connect()
File "/usr/local/lib/python3.5/dist-packages/httplib2/__init__.py", line 1291, in connect
self.sock = self._context.wrap_socket(sock, server_hostname=self.host)
File "/usr/lib/python3.5/ssl.py", line 377, in wrap_socket
_context=self)
File "/usr/lib/python3.5/ssl.py", line 752, in __init__
self.do_handshake()
File "/usr/lib/python3.5/ssl.py", line 988, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib/python3.5/ssl.py", line 633, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:645)我的代码:
import pygsheets
import sys
from pathlib import Path
# Import Arguments required for generating Configuration. If one is not set - Do not continue
# str(sys.argv[1]) # Argument denoting the Google Service Account API Authentication File
# str(sys.argv[2]) # Argument denoting the Google Sheets URL Key found when logging into the Google Sheets Manually via browser
# str(sys.argv[3]) # Argument denoting the Google Worksheet Name, for unique identification within the Google Spreadsheet
# str(sys.argv[4]) # Argument denoting the File Name and output destination
# Variable Definitions
try:
gServiceAccAuthFile = str(sys.argv[1])
gSheetKey = str(sys.argv[2])
gWorksheetName = str(sys.argv[3])
outputFile = str(sys.argv[4])
outputDir = str(Path(outputFile).parents[0])
outputFileName = str(Path(outputFile).stem)
except IndexError:
print('Not enough Arguments Specified, Required Arguments:\nPOS 1.)\tGoogle Service Account API Authentication File\nPOS 2.)\tGoogle Sheets URL Key\nPOS 3.)\tGoogle Worksheet Name\nPOS 4.)\tOutput File Path & Name')
sys.exit()
# Authorize Spreadsheet Access
gc = pygsheets.authorize(service_file=gServiceAccAuthFile, retries=1)
# Open spreadsheet
sh = gc.open_by_key(gSheetKey)
# Open Worksheet
wks = sh.worksheet_by_title(gWorksheetName)
# Export as CSV
wks.export(pygsheets.ExportType.CSV, path=outputDir + '/', filename=outputFileName)拟议解决办法:
except ssl.SSLEOFErrorwks.export() retry函数吗?发布于 2019-07-11 09:26:02
试/除:什么是“除”语句?除了ssl.SSLEOFError? Pygsheets:有wks.export()重试函数吗?
结合这些--我使用logging进行日志记录,但是可以根据您的需要进行调整。
import ssl
import logging
log = logging.getLogger(...)
for attempt in range(1, 6): # Try at most 5 times
try:
wks.export(pygsheets.ExportType.CSV, path=outputDir + '/', filename=outputFileName)
except ssl.SSLError as e:
log.warning('Attempt %d to export sheet failed: %s' % (attempt, e), exc_info=True)
else:
break # success!
else: # executed if we didn't `break` out
raise RuntimeError('All attempts to export the sheet failed!')https://stackoverflow.com/questions/56985814
复制相似问题