epicyon/session.py

45 lines
1.3 KiB
Python
Raw Normal View History

2019-06-28 18:55:29 +00:00
__filename__ = "session.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "0.0.1"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import requests
import json
def createSession(onionRoute: bool):
session = requests.session()
if onionRoute:
session.proxies = {}
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
return session
def getJson(session,url: str,headers,params):
sessionParams={}
sessionHeaders={}
if headers:
sessionHeaders=headers
if params:
sessionParams=params
sessionHeaders['User-agent'] = "HotJava/1.1.2 FCS"
session.cookies.clear()
return session.get(url, headers=sessionHeaders, params=sessionParams).json()
2019-06-28 19:36:39 +00:00
2019-06-28 20:22:36 +00:00
def postJson(session,postJson,allowedDomains,inboxUrl: str):
2019-06-28 19:36:39 +00:00
"""Post a json message to the inbox of another person
"""
2019-06-28 20:22:36 +00:00
# check that we are posting to a permitted domain
permittedDomain=False
for domain in allowedDomains:
if domain in inboxUrl:
permittedDomain=True
break
if not permittedDomain:
return None
2019-06-28 19:36:39 +00:00
postResult = session.post(url = inboxUrl, data = postJson)
return postResult.text