Exit from retries of unauthorized

main2
Bob Mottram 2019-10-23 19:44:03 +01:00
parent ef7135cd98
commit 21ce09e192
2 changed files with 14 additions and 8 deletions

View File

@ -1012,14 +1012,18 @@ def threadSendPost(session,postJsonStr: str,federationList: [],\
sendIntervalSec=30
for attempt in range(20):
postResult=None
unauthorized=False
try:
postResult = \
postResult,unauthorized = \
postJsonString(session,postJsonStr,federationList, \
inboxUrl,signatureHeaderJson, \
"inbox:write",debug)
except Exception as e:
print('ERROR: postJsonString failed')
print(e)
print('ERROR: postJsonString failed '+str(e))
if unauthorized==True:
print(postJsonStr)
print('threadSendPost: Post is unauthorized')
break
if postResult:
logStr='Success on try '+str(tries)+': '+postJsonStr
else:

View File

@ -75,9 +75,10 @@ def postJsonString(session,postJsonStr: str, \
inboxUrl: str, \
headers: {}, \
capability: str, \
debug: bool) -> bool:
debug: bool) -> (bool,bool):
"""Post a json message string to the inbox of another person
Supplying a capability, such as "inbox:write"
The second boolean returned is true if the send is unauthorized
NOTE: Here we post a string rather than the original json so that
conversions between string and json format don't invalidate
the message body digest of http signatures
@ -88,7 +89,7 @@ def postJsonString(session,postJsonStr: str, \
# check that we are posting to a permitted domain
if not urlPermitted(inboxUrl,federationList,capability):
print('postJson: '+inboxUrl+' not permitted by capabilities')
return None
return None,None
postResult = session.post(url = inboxUrl, data = postJsonStr, headers=headers)
if postResult.status_code<200 or postResult.status_code>202:
@ -97,13 +98,14 @@ def postJsonString(session,postJsonStr: str, \
# postResult = session.post(url = inboxUrl, data = postJsonStr, headers=headers)
# if not (postResult.status_code<200 or postResult.status_code>202):
# return True
if postResult.status_code==401:
if postResult.status_code>=400 and postResult.status_code<=405:
print('WARN: >>> Post to '+inboxUrl+' is unauthorized <<<')
return False,True
else:
print('WARN: Failed to post to '+inboxUrl+' with headers '+str(headers))
print('status code '+str(postResult.status_code))
return False
return True
return False,False
return True,False
def postImage(session,attachImageFilename: str,federationList: [],inboxUrl: str,headers: {},capability: str) -> str:
"""Post an image to the inbox of another person or outbox via c2s