Esempio n. 1
0
def application(environ, start_response):
    """
    Dispatches the server application through a handler. Specify a handler
    with the 'handler' decorator.
    """
    path = environ.get("PATH_INFO", "").lstrip("/").split("/")

    logger.info("Got HTTP request: {0}".format("/".join(path)))

    try:
        if DEBUG_SERVER and path[0] == 'server':
            path = path[1:]
        action = path[0]
    except IndexError:
        raise Error404("Missing action.")

    try:
        handler, type, jsonify, post, passenviron = handlers[action]
    except KeyError:
        start_response("200 OK", [("Content-Type", "text/plain")])
        return ["Error 404\n", "Action {0} undefined.".format(action)]

    try:
        args = path[1:]
        if post:
            length = int(environ.get('CONTENT_LENGTH', '0'))
            postdata = environ["wsgi.input"].read(length)
            if post == "json":
                args.append(json.loads(postdata))
            else:
                args.append(postdata)
        if passenviron:
            args.append(environ)
        try:
            response = handler(*args)
        finally:
            session.remove()
    except Error404 as e:
        start_response("404 Not Found", [("Content-Type", "text/plain")])
        return ["Error 404\n", str(e)]
    else:
        start_response("200 OK", [("Content-Type", type)])
        if jsonify:
            logger.debug("Response to " + str("/".join(path)) + ": " +
                         str(response))
            return [json.dumps(response)]
        else:
            return response
Esempio n. 2
0
def application(environ, start_response):
    """
    Dispatches the server application through a handler. Specify a handler
    with the 'handler' decorator.
    """
    path = environ.get("PATH_INFO", "").lstrip("/").split("/")

    logger.info("Got HTTP request: {0}".format("/".join(path)))

    try:
        action = path[0]
    except IndexError:
        raise Error404("Missing action.")

    try:
        handler, type, jsonify, post, passenviron = handlers[action]
    except KeyError:
        start_response("200 OK", [("Content-Type", "text/plain")])
        return ["Error 404\n", "Action {0} undefined.".format(action)]

    try:
        args = path[1:]
        if post:
            postdata = environ["wsgi.input"].read()
            if post == "json":
                args.append(json.loads(postdata))
            else:
                args.append(postdata)
        if passenviron:
            args.append(environ)
        try:
            response = handler(*args)
        finally:
            session.remove()
    except Error404 as e:
        start_response("404 Not Found", [("Content-Type", "text/plain")])
        return ["Error 404\n", str(e)]
    else:
        start_response("200 OK", [("Content-Type", type)])
        if jsonify:
            logger.debug("Response to " + str("/".join(path)) + ": " + str(response))
            return [json.dumps(response)]
        else:
            return response
Esempio n. 3
0
def main(args=None):
    """
    Dispatches the cli command through a given handler.
    """
    if args is None:
        args = sys.argv[1:]
    try:
        args[0]
    except IndexError:
        help()
    else:
        try:
            handler = handlers[args[0]][0]
        except KeyError:
            print "Error: Unknown action {0}".format(args[0])
        else:
            try:
                handler(args[1:])
            finally:
                if session:
                    session.remove()
Esempio n. 4
0
def main(args = None):
    """
    Dispatches the cli command through a given handler.
    """
    if args is None:
        args = sys.argv[1:]
    try:
        args[0]
    except IndexError:
        help()
    else:
        try:
           handler = handlers[args[0]][0]
        except KeyError:
            print "Error: Unknown action {0}".format(args[0])
        else:
            try:
                handler(args[1:])
            finally:
                if session:
                    session.remove()
Esempio n. 5
0
def application(environ, start_response):
    """
    Dispatches the server application through a handler. Specify a handler
    with the 'handler' decorator.
    """

    path = environ.get("PATH_INFO", "").lstrip("/").split("/")

    logger.info("Got HTTP request: {0}".format("/".join(path)))

    # file_target = open("/home/robolab/re3_server/log.txt", "a")
    # file_target.write(str(path) + "\n")
    # file_target.flush()
    # file_target.close()

    try:
        if DEBUG_SERVER and path[0] == 'server':
            path = path[1:]
        action = path[0]
    except IndexError:
        raise Error404("Missing action.")

    # file_target = open("/home/sujiez/re3_server_client/server_data.txt", 'a')
    try:
        handler, type, jsonify, post, passenviron = handlers[action]
    except KeyError:
        start_response("200 OK", [("Content-Type", "text/plain")])
        return ["Error 404\n", "Action {0} undefined.".format(action)]

    try:

        # file_target.write("\n\npath is: " + str(path) + "\n\n")

        args = path[1:]

        # file_target.write("\n\nargs is: " + str(args) + "\n\n")

        if post:
            length = int(environ.get('CONTENT_LENGTH', '0'))
            postdata = environ["wsgi.input"].read(length)

            # file_target.write("\n\npostdata is: " + str(postdata) + "\n\n")

            if post == "json":
                args.append(json.loads(postdata))
            else:
                args.append(postdata)

        if passenviron:
            # file_target.write("\n\nenv is: " + str(environ) + "\n\n")
            args.append(environ)

        try:
            # file_target.write("\n\nhandler is: " + str(handler) + "\n\n")

            response = handler(*args)
        finally:

            # file_target.flush()
            # file_target.close()
            session.remove()
    except Error404 as e:
        start_response("404 Not Found", [("Content-Type", "text/plain")])
        return ["Error 404\n", str(e)]
    else:
        start_response("200 OK", [("Content-Type", type)])
        if jsonify:
            logger.debug("Response to " + str("/".join(path)) + ": " +
                         str(response))
            return [json.dumps(response)]
        else:
            return response