Example #1
0
def execute(method, params):
    global fifo_file

    jsoncmd = jsonrpc_helper.get_command(method, params)
    reply_fifo_file_name = REPLY_FIFO_FILE_TEMPLATE.format(random.randrange(32767))
    reply_dir = cfg.get('fifo_reply_dir')
    reply_fifo_file = "{}/{}".format(reply_dir, reply_fifo_file_name)

    # make sure fifo file does not exist
    try:
        os.unlink(reply_fifo_file)
        logger.debug("removed reply fifo '{}'".format(reply_fifo_file))
    except OSError as ex:
        if os.path.exists(reply_fifo_file):
            raise jsonrpc_helper.JSONRPCException(
                    "cannot remove repl file {}: {}!".
                    format(reply_fifo_file, ex))

    try:
        os.mkfifo(reply_fifo_file)
        os.chmod(reply_fifo_file, 0o666)
    except OSError as ex:
        raise jsonrpc_helper.JSONRPCException(
                "cannot create reply file {}: {}!".
                format(reply_fifo_file, ex))

    fifocmd = ":{}:{}". format(reply_fifo_file_name, jsoncmd)
    try:
        fifo = open(fifo_file, 'w')
        fifo.write(fifocmd)
        logger.debug("sent command '{}'".format(fifocmd))
        fifo.close()
    except Exception as ex:
        raise jsonrpc_helper.JSONRPCException(
                "cannot access fifo file {}: {}!".
                format(fifo_file, ex))

    logger.debug("reply file '{}'".format(reply_fifo_file))
    with open(reply_fifo_file, 'r') as reply_fifo:
        replycmd = reply_fifo.readline()
        #logger.debug("received reply '{}'".format(replycmd))

    # TODO: should we add this in a loop?
    os.unlink(reply_fifo_file)
    return jsonrpc_helper.get_reply(replycmd)
Example #2
0
def execute(method, params):
    url = cfg.get('url')
    jsoncmd = jsonrpc_helper.get_command(method, params)
    headers = {'Content-Type': 'application/json'}
    request = urllib.request.Request(url, jsoncmd.encode(), headers)
    url_parsed = urllib.parse.urlparse(url)
    try:
        if url_parsed.scheme == 'https':
            replycmd = urllib.request.urlopen(
                request,
                context=ssl._create_unverified_context()).read().decode()
        else:
            replycmd = urllib.request.urlopen(request).read().decode()
    except urllib.error.HTTPError as e:
        raise jsonrpc_helper.JSONRPCException(str(e))
    return jsonrpc_helper.get_reply(replycmd)