flake8 format

main
Bob Mottram 2020-04-04 12:00:19 +01:00
parent bfe37d5ae7
commit d652485f8a
1 changed files with 78 additions and 76 deletions

View File

@ -7,13 +7,13 @@ __email__="bob@freedombone.net"
__status__ = "Production" __status__ = "Production"
import os import os
import sys
import requests import requests
from utils import urlPermitted from utils import urlPermitted
import json import json
baseDirectory = None baseDirectory = None
def createSession(onionRoute: bool): def createSession(onionRoute: bool):
session = requests.session() session = requests.session()
if onionRoute: if onionRoute:
@ -22,8 +22,10 @@ def createSession(onionRoute: bool):
session.proxies['https'] = 'socks5h://localhost:9050' session.proxies['https'] = 'socks5h://localhost:9050'
return session return session
def getJson(session,url: str,headers: {},params: {}, \
version='1.0.0',httpPrefix='https',domain='testdomain') -> {}: def getJson(session, url: str, headers: {}, params: {},
version='1.0.0', httpPrefix='https',
domain='testdomain') -> {}:
if not isinstance(url, str): if not isinstance(url, str):
print('url: ' + str(url)) print('url: ' + str(url))
print('ERROR: getJson url should be a string') print('ERROR: getJson url should be a string')
@ -36,11 +38,10 @@ def getJson(session,url: str,headers: {},params: {}, \
sessionParams = params sessionParams = params
sessionHeaders['User-Agent'] = 'Epicyon/' + version sessionHeaders['User-Agent'] = 'Epicyon/' + version
if domain: if domain:
sessionHeaders['User-Agent']+='; +'+httpPrefix+'://'+domain+'/' sessionHeaders['User-Agent'] += \
#"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5)" '; +' + httpPrefix + '://' + domain + '/'
if not session: if not session:
print('WARN: no session specified for getJson') print('WARN: no session specified for getJson')
#session.cookies.clear()
try: try:
result = session.get(url, headers=sessionHeaders, params=sessionParams) result = session.get(url, headers=sessionHeaders, params=sessionParams)
return result.json() return result.json()
@ -52,12 +53,12 @@ def getJson(session,url: str,headers: {},params: {}, \
print(e) print(e)
return None return None
def postJson(session,postJsonObject: {},federationList: [], \
def postJson(session, postJsonObject: {}, federationList: [],
inboxUrl: str, headers: {}, capability: str) -> str: inboxUrl: str, headers: {}, capability: str) -> str:
"""Post a json message to the inbox of another person """Post a json message to the inbox of another person
Supplying a capability, such as "inbox:write" Supplying a capability, such as "inbox:write"
""" """
# always allow capability requests # always allow capability requests
if not capability.startswith('cap'): if not capability.startswith('cap'):
# check that we are posting to a permitted domain # check that we are posting to a permitted domain
@ -66,18 +67,19 @@ def postJson(session,postJsonObject: {},federationList: [], \
return None return None
postResult = \ postResult = \
session.post(url=inboxUrl, \ session.post(url=inboxUrl,
data=json.dumps(postJsonObject), \ data=json.dumps(postJsonObject),
headers=headers) headers=headers)
if postResult: if postResult:
return postResult.text return postResult.text
return None return None
def postJsonString(session,postJsonStr: str, \
federationList: [], \ def postJsonString(session, postJsonStr: str,
inboxUrl: str, \ federationList: [],
headers: {}, \ inboxUrl: str,
capability: str, \ headers: {},
capability: str,
debug: bool) -> (bool, bool): debug: bool) -> (bool, bool):
"""Post a json message string to the inbox of another person """Post a json message string to the inbox of another person
Supplying a capability, such as "inbox:write" Supplying a capability, such as "inbox:write"
@ -86,7 +88,6 @@ def postJsonString(session,postJsonStr: str, \
conversions between string and json format don't invalidate conversions between string and json format don't invalidate
the message body digest of http signatures the message body digest of http signatures
""" """
# always allow capability requests # always allow capability requests
if not capability.startswith('cap'): if not capability.startswith('cap'):
# check that we are posting to a permitted domain # check that we are posting to a permitted domain
@ -97,22 +98,22 @@ def postJsonString(session,postJsonStr: str, \
postResult = \ postResult = \
session.post(url=inboxUrl, data=postJsonStr, headers=headers) session.post(url=inboxUrl, data=postJsonStr, headers=headers)
if postResult.status_code < 200 or postResult.status_code > 202: if postResult.status_code < 200 or postResult.status_code > 202:
#if postResult.status_code==400: if postResult.status_code >= 400 and \
# headers['content-type']='application/ld+json' postResult.status_code <= 405 and \
# 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>=400 and postResult.status_code<=405 and \
postResult.status_code != 404: postResult.status_code != 404:
print('WARN: >>> Post to '+inboxUrl+' is unauthorized. Code '+str(postResult.status_code)+' <<<') print('WARN: >>> Post to ' + inboxUrl +
' is unauthorized. Code ' +
str(postResult.status_code) + ' <<<')
return False, True return False, True
else: else:
print('WARN: Failed to post to '+inboxUrl+' with headers '+str(headers)) print('WARN: Failed to post to ' + inboxUrl +
' with headers ' + str(headers))
print('status code ' + str(postResult.status_code)) print('status code ' + str(postResult.status_code))
return False, False return False, False
return True, False return True, False
def postImage(session,attachImageFilename: str,federationList: [], \
def postImage(session, attachImageFilename: str, federationList: [],
inboxUrl: str, headers: {}, capability: str) -> str: inboxUrl: str, headers: {}, capability: str) -> str:
"""Post an image to the inbox of another person or outbox via c2s """Post an image to the inbox of another person or outbox via c2s
Supplying a capability, such as "inbox:write" Supplying a capability, such as "inbox:write"
@ -124,9 +125,9 @@ def postImage(session,attachImageFilename: str,federationList: [], \
print('postJson: ' + inboxUrl + ' not permitted') print('postJson: ' + inboxUrl + ' not permitted')
return None return None
if not (attachImageFilename.endswith('.jpg') or \ if not (attachImageFilename.endswith('.jpg') or
attachImageFilename.endswith('.jpeg') or \ attachImageFilename.endswith('.jpeg') or
attachImageFilename.endswith('.png') or \ attachImageFilename.endswith('.png') or
attachImageFilename.endswith('.gif')): attachImageFilename.endswith('.gif')):
print('Image must be png, jpg, or gif') print('Image must be png, jpg, or gif')
return None return None
@ -142,7 +143,8 @@ def postImage(session,attachImageFilename: str,federationList: [], \
with open(attachImageFilename, 'rb') as avFile: with open(attachImageFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
postResult=session.post(url=inboxUrl,data=mediaBinary,headers=headers) postResult = session.post(url=inboxUrl, data=mediaBinary,
headers=headers)
if postResult: if postResult:
return postResult.text return postResult.text
return None return None