From 9e51c7fba9179fb6db5fe131df3ed574a94f08d1 Mon Sep 17 00:00:00 2001 From: Ethan Baer Date: Thu, 6 Aug 2015 17:11:00 -0500 Subject: [PATCH] added session rebuilding to test possible fix for requests.ConnectionError happening after long sleep --- twython/api.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/twython/api.py b/twython/api.py index 6046f13..321cf63 100644 --- a/twython/api.py +++ b/twython/api.py @@ -8,8 +8,8 @@ This module contains functionality for access to core Twitter API calls, Twitter Authentication, and miscellaneous methods that are useful when dealing with the Twitter API """ - import warnings +import logging import requests from requests.auth import HTTPBasicAuth @@ -25,7 +25,6 @@ from .helpers import _transparent_params warnings.simplefilter('always', TwythonDeprecationWarning) # For Python 2.7 > - class Twython(EndpointsMixin, object): def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, access_token=None, @@ -163,7 +162,23 @@ class Twython(EndpointsMixin, object): try: response = func(url, **requests_args) except requests.RequestException as e: - raise TwythonError(str(e)) + logging.exception('RequestException encountered - refreshing...') + # Rebuild Session and retry + s2 = requests.Session() + s2.auth = self.client.auth + s2.headers = self.client.headers + s2.cert = self.client.cert + s2.hooks = self.client.hooks + s2.proxies = self.client.proxies + s2.max_redirects = self.client.max_redirects + self.client.close() + + self.client = s2 + try: + response = func(url, **requests_args) + except requests.RequestException as e: + logging.exception('RequestException encountered after refresh!') + raise TwythonError(str(e)) # create stash for last function intel self._last_call = { -- 2.39.5