python gspread谷歌电子表格保持连接活着

时间:2014-05-09 15:50:05

标签: python gspread

我正在使用gspread更新我的电子表格,这个过程大约需要一个小时,我有大约200个电子表格。似乎大约30分钟更新表格,连接下降。有没有办法保持登录活着?我以为我保持连接活着,因为我每隔30秒就打开并写入不同的纸张。

我可以使用try语句,如果它炸弹重新登录。我想知道是否有人有更好的方法?

我曾经使用gspread示例中的简单示例:

gc = gspread.login('thedude@abid.es', 'password')
sht1 = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')

如何将其转换为保持连接登录以到达sht1

1 个答案:

答案 0 :(得分:4)

为了保持连接,你应该使用持久连接。

所以,如果你查看主文件:

http://burnash.github.io/gspread/#gspread.Client

您会看到gspread.login方法是Client的实例。并且Client可以接受http标题。

http://burnash.github.io/gspread/#gspread.httpsession.HTTPSession

现在在您的连接中添加此标头:Connection: Keep-Alive

import gspread
headers = gspread.httpsession.HTTPSession(headers={'Connection':'Keep-Alive'})
con = gspread.Client(auth=('you@gmail.com','password'),http_session=headers)
con.login()
con.open_by_key('....')

然后当你打印会话标题时:

print con.session.headers
Out[5]: {'Authorization': u'GoogleLogin auth=xxxxxxx', 'Connection': 'Keep-Alive'}

对于持久连接的详细信息,请查看以下链接:

http://en.wikipedia.org/wiki/HTTP_persistent_connection

http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html

有关gspread httpsession的代码详细信息,请查看:

https://github.com/burnash/gspread/blob/master/gspread/httpsession.py

相关问题