From 72fdb90d17071d2f9d2d723a097aed4fc5dddbc6 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 23 Jun 2020 14:28:41 +0100 Subject: [PATCH] Try trapping socket errors --- session.py | 25 +++++++++++++++++++------ utils.py | 6 +++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/session.py b/session.py index 31de91ac8..332b99b69 100644 --- a/session.py +++ b/session.py @@ -10,6 +10,8 @@ import os import requests from utils import urlPermitted import json +from socket import error as SocketError +import errno baseDirectory = None @@ -18,8 +20,11 @@ def createSession(proxyType: str): session = None try: session = requests.session() - except BaseException: - print('ERROR: session request failed') + except SocketError as e: + if e.errno == errno.ECONNRESET: + print('WARN: connection was reset during createSession') + else: + print('WARN: session request failed') return None if not session: return None @@ -60,7 +65,9 @@ def getJson(session, url: str, headers: {}, params: {}, try: result = session.get(url, headers=sessionHeaders, params=sessionParams) return result.json() - except Exception as e: + except SocketError as e: + if e.errno == errno.ECONNRESET: + print('WARN: connection was reset during getJson') print('ERROR: getJson failed\nurl: ' + str(url) + '\n' + 'headers: ' + str(sessionHeaders) + '\n' + 'params: ' + str(sessionParams) + '\n') @@ -85,7 +92,9 @@ def postJson(session, postJsonObject: {}, federationList: [], session.post(url=inboxUrl, data=json.dumps(postJsonObject), headers=headers) - except BaseException: + except SocketError as e: + if e.errno == errno.ECONNRESET: + print('WARN: connection was reset during postJson') print('ERROR: postJson failed ' + inboxUrl + ' ' + json.dumps(postJsonObject) + ' ' + str(headers)) return None @@ -117,7 +126,9 @@ def postJsonString(session, postJsonStr: str, try: postResult = \ session.post(url=inboxUrl, data=postJsonStr, headers=headers) - except BaseException: + except SocketError as e: + if e.errno == errno.ECONNRESET: + print('WARN: connection was reset during postJsonString') print('ERROR: postJsonString failed ' + inboxUrl + ' ' + postJsonStr + ' ' + str(headers)) return None, None @@ -170,7 +181,9 @@ def postImage(session, attachImageFilename: str, federationList: [], try: postResult = session.post(url=inboxUrl, data=mediaBinary, headers=headers) - except BaseException: + except SocketError as e: + if e.errno == errno.ECONNRESET: + print('WARN: connection was reset during postImage') print('ERROR: postImage failed ' + inboxUrl + ' ' + str(headers)) return None diff --git a/utils.py b/utils.py index fbe06ee95..944ebb60a 100644 --- a/utils.py +++ b/utils.py @@ -11,6 +11,8 @@ import time import shutil import datetime import json +from socket import error as SocketError +import errno from urllib.request import urlopen from pprint import pprint from calendar import monthrange @@ -1077,5 +1079,7 @@ def siteIsActive(url: str) -> bool: try: urlopen(url, timeout=10) return True - except BaseException: + except SocketError as e: + if e.errno == errno.ECONNRESET: + print('WARN: connection was reset during siteIsActive') return False