Python 3间断ssl.SSLEOFError
Python 3 intermittent ssl.SSLEOFError
我每 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)
建议的解决方案:
- SSL 模块问题:更新到较新的二进制文件?
- Try / except:except 语句是什么?
except ssl.SSLEOFError
?
- Pygsheets:是否有
wks.export()
retry
函数?
Try / except: What would be the except statement? except ssl.SSLEOFError?
Pygsheets: Is there a wks.export() retry function?
结合这些 – 我正在使用 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!')
我每 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)
建议的解决方案:
- SSL 模块问题:更新到较新的二进制文件?
- Try / except:except 语句是什么?
except ssl.SSLEOFError
? - Pygsheets:是否有
wks.export()
retry
函数?
Try / except: What would be the except statement? except ssl.SSLEOFError?
Pygsheets: Is there a wks.export() retry function?
结合这些 – 我正在使用 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!')