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('thedude@abid.es', 'password')
sht1 = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')

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

jason
  • 3,811
  • 18
  • 92
  • 147

1 Answers1

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=('you@gmail.com','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

mortymacs
  • 3,456
  • 3
  • 27
  • 53
  • 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. – jason May 09 '14 at 17:44
  • @jason_cant_code sure! I updated my post. check it ;) – mortymacs May 09 '14 at 18:01
  • @jason_cant_code have a look into persistent connection image: http://en.wikipedia.org/wiki/File:HTTP_persistent_connection.svg – mortymacs May 09 '14 at 18:13
  • so the connection will be alive til my program is finished? how does it "close"? – jason May 09 '14 at 18:22
  • @jason_cant_code could you update your post and add your codes? – mortymacs May 09 '14 at 18:31
  • 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'` – jason May 09 '14 at 18:33
  • @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? – mortymacs May 09 '14 at 18:59
  • I see it should be `con.open_by_key` not `con.login().open_by_key` – jason May 09 '14 at 19:07
  • @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: http://burnash.github.io/gspread/#gspread.Client.open_by_key – mortymacs May 09 '14 at 19:15
  • @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. – mortymacs May 09 '14 at 19:26
  • 1
    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... – DataSwede Jul 01 '14 at 14:08
  • 1
    This method of authentication isn't supported anymore by Google. see this link https://developers.google.com/identity/protocols/OAuth2 . – Richard de Ree Apr 24 '16 at 16:25
  • 3
    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()` – PKaura May 03 '16 at 05:28