Try trapping socket errors

main
Bob Mottram 2020-06-23 14:28:41 +01:00
parent 34d66a28b2
commit 72fdb90d17
2 changed files with 24 additions and 7 deletions

View File

@ -10,6 +10,8 @@ import os
import requests import requests
from utils import urlPermitted from utils import urlPermitted
import json import json
from socket import error as SocketError
import errno
baseDirectory = None baseDirectory = None
@ -18,8 +20,11 @@ def createSession(proxyType: str):
session = None session = None
try: try:
session = requests.session() session = requests.session()
except BaseException: except SocketError as e:
print('ERROR: session request failed') if e.errno == errno.ECONNRESET:
print('WARN: connection was reset during createSession')
else:
print('WARN: session request failed')
return None return None
if not session: if not session:
return None return None
@ -60,7 +65,9 @@ def getJson(session, url: str, headers: {}, params: {},
try: try:
result = session.get(url, headers=sessionHeaders, params=sessionParams) result = session.get(url, headers=sessionHeaders, params=sessionParams)
return result.json() 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' + print('ERROR: getJson failed\nurl: ' + str(url) + '\n' +
'headers: ' + str(sessionHeaders) + '\n' + 'headers: ' + str(sessionHeaders) + '\n' +
'params: ' + str(sessionParams) + '\n') 'params: ' + str(sessionParams) + '\n')
@ -85,7 +92,9 @@ def postJson(session, postJsonObject: {}, federationList: [],
session.post(url=inboxUrl, session.post(url=inboxUrl,
data=json.dumps(postJsonObject), data=json.dumps(postJsonObject),
headers=headers) 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 + ' ' + print('ERROR: postJson failed ' + inboxUrl + ' ' +
json.dumps(postJsonObject) + ' ' + str(headers)) json.dumps(postJsonObject) + ' ' + str(headers))
return None return None
@ -117,7 +126,9 @@ def postJsonString(session, postJsonStr: str,
try: try:
postResult = \ postResult = \
session.post(url=inboxUrl, data=postJsonStr, headers=headers) 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 + ' ' + print('ERROR: postJsonString failed ' + inboxUrl + ' ' +
postJsonStr + ' ' + str(headers)) postJsonStr + ' ' + str(headers))
return None, None return None, None
@ -170,7 +181,9 @@ def postImage(session, attachImageFilename: str, federationList: [],
try: try:
postResult = session.post(url=inboxUrl, data=mediaBinary, postResult = session.post(url=inboxUrl, data=mediaBinary,
headers=headers) 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 + ' ' + print('ERROR: postImage failed ' + inboxUrl + ' ' +
str(headers)) str(headers))
return None return None

View File

@ -11,6 +11,8 @@ import time
import shutil import shutil
import datetime import datetime
import json import json
from socket import error as SocketError
import errno
from urllib.request import urlopen from urllib.request import urlopen
from pprint import pprint from pprint import pprint
from calendar import monthrange from calendar import monthrange
@ -1077,5 +1079,7 @@ def siteIsActive(url: str) -> bool:
try: try:
urlopen(url, timeout=10) urlopen(url, timeout=10)
return True return True
except BaseException: except SocketError as e:
if e.errno == errno.ECONNRESET:
print('WARN: connection was reset during siteIsActive')
return False return False