json mode added to local command

This commit is contained in:
Kevin Froman 2020-01-03 04:15:20 -06:00
parent d7be2ca16d
commit 6529d3e622

View File

@ -17,7 +17,11 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import urllib, requests, time import urllib, time
import json
import requests
import logger, config, deadsimplekv import logger, config, deadsimplekv
from . import getclientapiserver from . import getclientapiserver
import filepaths import filepaths
@ -46,20 +50,37 @@ def get_hostname():
else: else:
return hostname return hostname
def local_command(command, data='', silent = True, post=False, postData = {}, maxWait=20): def local_command(command, data='', silent = True, post=False,
postData = {}, maxWait=20,
is_json=False
):
''' '''
Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers. Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers.
''' '''
# TODO: URL encode parameters, just as an extra measure. May not be needed, but should be added regardless. # TODO: URL encode parameters, just as an extra measure. May not be needed, but should be added regardless.
hostname = get_hostname() hostname = get_hostname()
if hostname == False: return False if hostname == False: return False
if data != '': if data != '':
data = '&data=' + urllib.parse.quote_plus(data) data = '&data=' + urllib.parse.quote_plus(data)
payload = 'http://%s/%s%s' % (hostname, command, data) payload = 'http://%s/%s%s' % (hostname, command, data)
try: try:
if post: if post:
ret_data = requests.post(payload, data=postData, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text if is_json:
ret_data = requests.post(
payload,
json=postData,
headers={'token': config.get('client.webpassword'),
'Connection': 'close'},
timeout=(maxWait, maxWait)).text
else:
ret_data = requests.post(
payload,
data=postData,
headers={'token': config.get('client.webpassword'),
'Connection': 'close'},
timeout=(maxWait, maxWait)).text
else: else:
ret_data = requests.get(payload, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text ret_data = requests.get(payload, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text
except Exception as error: except Exception as error: