使用 Python 将文件上传到 Google Drive 时出错

error uploading file to google drive with python

提问人:flierd 提问时间:12/17/2022 最后编辑:flierd 更新时间:12/22/2022 访问量:259

问:

我写了一个代码来上传(创建和更新)一个文件到谷歌驱动器, 在带有 python 3.9 的 Windows 10 中它可以工作,但在带有 python 3.8 的 Windows 2008 服务器中它给了我一个错误。

请记住 3.8 是支持 Windows 2008 的最高版本

如果我尝试从 gdrive 列出它的工作,问题只是创建或更新文件。 请记住,3.8 是支持 Windows 2008 的最后一个 Python 版本。 我怀疑它可能与 Windows 2008 和 ssl 有关?!?

错误是这样的:

C:\backupmgr>python drive.py
Traceback (most recent call last):
  File "drive.py", line 112, in <module>
    envia_zip('sexta.7z')
  File "drive.py", line 104, in envia_zip
    file = service.files().create(body=file_metadata, media_body=media).execute(
)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\googleapiclient\http.py", line 923, in execute
    resp, content = _retry_request(
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\googleapiclient\http.py", line 222, in _retry_request
    raise exception
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\googleapiclient\http.py", line 191, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\google_auth_httplib2.py", line 218, in request
    response, content = self.http.request(
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\httplib2\__init__.py", line 1720, in request
    (response, content) = self._request(
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\httplib2\__init__.py", line 1440, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, he
aders)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-p
ackages\httplib2\__init__.py", line 1363, in _conn_request
    conn.request(method, request_uri, body, headers)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 1252, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 1298, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 1247, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 1046, in _send_output
    self.send(chunk)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\http\c
lient.py", line 968, in send
    self.sock.sendall(data)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\ssl.py
", line 1204, in sendall
    v = self.send(byte_view[count:])
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\ssl.py
", line 1173, in send
    return self._sslobj.write(data)
socket.timeout: The write operation timed out

蟒蛇 python-3.x google-drive-api windows-server-2008-r2

评论

0赞 Norhther 12/17/2022
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用。请参阅:如何创建最小、可重现的示例
0赞 Andrew Ryan 12/17/2022
尝试分解每个步骤并单独尝试时发生错误?清楚地看到错误来自哪里,因为单个 LIN 有多个点运算符,因此不清楚该行中的错误在哪里。file = service.files().create(body=file_metadata, media_body=media).execute()
0赞 Community 12/17/2022
请提供足够的代码,以便其他人可以更好地理解或重现问题。
1赞 Linda Lawton - DaImTo 12/17/2022
好奇。该库支持 仅支持 python 3.7 及更高版本。 所以使用 3.8 你应该很好。对我来说,这听起来像是一个网络错误,发现了这个 #632 和这个 1080

答:

1赞 flierd 12/17/2022 #1

好吧,它现在可以工作了,正如@DaImTo在 google api github 中提出的问题 #632 一样,这不是 api 的问题。问题是套接字核心模块的默认超时较低。我使用的装有 Windows Server 2008 的 PC 非常慢,并且达到了这个默认超时,所以我只是通过在脚本的开头插入代码来增加默认超时:

import socket
socket.setdefaulttimeout(600)

评论

0赞 Community 12/20/2022
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。