python gspread google spreadsheet keeping connection alive
Asked Answered
B

1

6

I'm updating my spreadsheets using gspread, the process takes about an hour, i have about 200 spreadsheets. It seems about 30 minutes into the updating the sheets, the connection drops. Is there a way to keep the login alive? I thought I was keeping the connection alive because I'm opening and writing to different sheets about every 30 seconds.

I can use a try statement and if it bombs re-login. I was wondering if anybody had a better way?

I'm used to using the simple example from gspread example of:

gc = gspread.login('[email protected]', 'password')
sht1 = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')

How do I turn this into a keep alive connection login to arrive at sht1?

Beta answered 9/5, 2014 at 15:50 Comment(0)
B
4

For keeping alive connection you should use persistent connection.

So if you check main document:

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

You will see the gspread.login method is instance of Client. and Client can accept http headers.

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

Now add this header in your connection : Connection: Keep-Alive

import gspread
headers = gspread.httpsession.HTTPSession(headers={'Connection':'Keep-Alive'})
con = gspread.Client(auth=('[email protected]','password'),http_session=headers)
con.login()
con.open_by_key('....')

Then when you get print of session headers:

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

For persistent connection details have a look into these links:

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

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

For codes details of gspread httpsession have a look into:

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

Bitstock answered 9/5, 2014 at 16:22 Comment(13)
can you show me a few lines of code for example? how do i login with persistent connection? I looked thru the link and still don't understand. I did try to dig through the code.Beta
@jason_cant_code sure! I updated my post. check it ;)Bitstock
@jason_cant_code have a look into persistent connection image: en.wikipedia.org/wiki/File:HTTP_persistent_connection.svgBitstock
so the connection will be alive til my program is finished? how does it "close"?Beta
@jason_cant_code could you update your post and add your codes?Bitstock
how do i then open_by_key? I have this code self.gc=con.login() self.cDB=self.gc.open_by_key(Gsheets.cDB_key) getting this error AttributeError: 'NoneType' object has no attribute 'open_by_key'Beta
@jason_cant_code that error refers to your coding. just try simple and check it. I tried it via ipython , it shows me open_by_key and open_by_url for con variable. did you test my code?Bitstock
I see it should be con.open_by_key not con.login().open_by_keyBeta
@jason_cant_code yes. it has open_by_key for con variable. but you need to login it before as I see in this example: burnash.github.io/gspread/#gspread.Client.open_by_keyBitstock
@jason_cant_code yes. you are right. first login and then open_by_key. never do them at the same time. I updated my post.Bitstock
Even after setting up the connection as a persistent connection Google will come back with a bad service line after enough time. Attempting to re-login with the con.login() does not solve it. Restarting the script will for a minute or two...Barbiturate
This method of authentication isn't supported anymore by Google. see this link developers.google.com/identity/protocols/OAuth2 .Apocrypha
Try this code: import gspread; from oauth2client.service_account import ServiceAccountCredentials; scope = ['https://spreadsheets.google.com/feeds']; credentials = ServiceAccountCredentials.from_json_keyfile_name('Pluss Inventory-7160481b4bc0.json', scope); headers = gspread.httpsession.HTTPSession(headers={'Connection': 'Keep-Alive'}); gc = gspread.Client(auth=credentials, http_session=headers); gc.login()Attaint

© 2022 - 2024 — McMap. All rights reserved.