Python 3 прерывистый ssl.SSLEOFError - PullRequest
0 голосов
/ 11 июля 2019

Я делаю выборку из листов Google, используя pygsheets модуль Python каждые 90 секунд.

В ранние утренние часы (обычно между 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: обновление до нового двоичного файла?
  • Попробуйте / за исключением: что будет за исключением заявления? except ssl.SSLEOFError
  • Pygsheets: Есть ли функция wks.export() retry?

1 Ответ

1 голос
/ 11 июля 2019

Попробуйте / за исключением: что было бы за исключением заявления? кроме 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!')
...