写入Google表格时,python gspread连接超时 [英] python gspread connection time out when writing to google sheets

查看:143
本文介绍了写入Google表格时,python gspread连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现有的连接代码,可以正常工作

here's my existing connection code, which works fine

json_key = json.load(open('path/to/json.js')) 
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
gc = gspread.authorize(credentials)

但在10分钟后,我的连接将超时。

but after 10 mintutes or so my connection will time out.

在阅读了一些内容后,我在连接代码中遇到了这个用法,

after reading around a bit i came across the use of this in the connection code,

headers = gspread.httpsession.HTTPSession(headers={'Connection':'Keep-Alive'})

,但我不确定如何将其包含在现有连接代码中。包含它的正确方法是什么?我见过的示例与上面的连接代码不同,并且这些示例指定了我使用的json连接文件中没有的密码

but i'm not sure how include into my existing connection code. what is the correct way to include it? the examples i've seen are not the same as my connection code above, and the examples specify a password that's not in the json connection file that i'm using

错误的输出。有时它们略有不同。但这全都归因于相同的连接超时

here's an an output of the error. sometimes they differ slightly. but it's all down to the same connection time out

File "/usr/local/lib/python2.7/dist-packages/gspread/models.py", line 429, in update_acell
return self.update_cell(*(self.get_int_addr(label)), val=val)
File "/usr/local/lib/python2.7/dist-packages/gspread/models.py", line 440, in update_cell
self._cell_addr(row, col))
File "/usr/local/lib/python2.7/dist-packages/gspread/client.py", line 271, in get_cells_cell_id_feed
r = self.session.get(url)
File "/usr/local/lib/python2.7/dist-packages/gspread/httpsession.py", line 79, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/gspread/httpsession.py", line 75, in request
raise HTTPError("%s: %s" % (response.status, response.read()))


推荐答案

如果超过了时间,则可以调用函数以重新连接到数据库。
我提供了一个示例代码片段,您可以将其合并到程序中。

If the time is exceeded, you can call a Function to Reconnect to the Database. I have provided a sample code snippet that you can incorporate within your program.

import gspread
from oauth2client.service_account import ServiceAccountCredentials

from datetime import datetime
from datetime import timedelta

def connect_database():

    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
    client = gspread.authorize(creds)

    workbook1 = client.open("File_Name")
    w1 = workbook1.worksheet("Sheet1")
    w2 = workbook1.worksheet("Sheet2")

    return w1,w2

program_start_time=datetime.now()

if datetime.now() > program_start_time + timedelta(minutes=10):
    worksheet1, worksheet2 = connect_database()
    program_start_time = datetime.now()   #Resets the Program Start Time

这篇关于写入Google表格时,python gspread连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆