0

I'm using the Gmail API to pull all the emails in a user's inbox. Everything works fine until about an hour, when the request stops returning a response with messages.

I use this code to request message headers:

sess = requests.Session()
retries = Retry(total=5, connect=10, backoff_factor=0.1, status_forcelist=[401, 500, 502, 503, 504], raise_on_redirect=True, raise_on_status=True)
sess.mount('https://', HTTPAdapter(max_retries=retries))

try:
            msg = json.loads(sess.get(message_url, params={'format': 'metadata'}, headers=authorization_header).text)
            logger.info('msg', dict(msg=msg))

except requests.HTTPError as e:
            logger.info('error', dict(
                error=e,
                error_code=e.code
            ))

except:
            logger.info('Unknown error')

And after an hour I'll get an error like so:

[2017-01-08 16:34:17,536: WARNING/PoolWorker-1] 
Retrying (Retry(total=4, connect=10, read=None, redirect=None)) 
after connection broken by 'ProtocolError('Connection aborted.', 
OSError(50, 'Network is down'))': 
/gmail/v1/users/<user_id>/messages/<message_id>?format=metadata

I thought I might be running into the issue because the access token expires, so I wrote a script to use the refresh token to get a new access token. The process also never catches the HTTPError, but instead prints 'Unknown error', so I don't think HTTPError would be the case.

Do you have any thoughts on how to approach this issue?

Adrienne
  • 2,540
  • 1
  • 29
  • 39

1 Answers1

1

Your error log says:

connection broken by 'ProtocolError('Connection aborted.', OSError(50, 'Network is down'))': 

'Network is down' may have something to do with unstable internet connection.

Another possible cause is your using the wrong URL. In this SO thread, the connection might be for http and not https, vice-versa.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • 1
    I'm using the Retry class from the requests library in Python to retry on ConnectionError, so I'm surprised I'm still getting that. I think I will write my own retry class and then increase the max retry threshold. – Adrienne Jan 14 '17 at 23:30