Beispiel #1
0
def main():
    args = get_cli_arguments(for_client=True)
    config = get_config(args.config)

    if not os.path.exists(args.file):
        sys.exit("error: file doesn't exists: {}".format(args.file))

    if not 'client' in config:
        sys.exit("error: client section is empty on your config file.")

    request_parameters = {}

    if 'user' in config.get("client") and 'pass' in config.get("client"):
        request_parameters.update({
            "auth": requests.auth.HTTPBasicAuth(config["client"]["user"], config["client"]["pass"]),
        })

    with open(args.file, 'r') as payload:
        request_parameters.update({
            "files": {"file": payload},
        })
        try:
            r = requests.post(config["client"]["endpoint"], **request_parameters)
        except requests.exceptions.ConnectionError:
            # @todo: how exactly requests library seperate requests with bad auth and requests with bad url?
            sys.exit("invalid URL or invalid credentials.")

        if r.status_code == 201:
            print "file successfully uploaded. {}{}".format(config.get("SERVE_URL"), r.content)
        else:
            sys.exit("error: {}".format(r.content))
Beispiel #2
0
def main():

    args = get_cli_arguments()
    config = get_config(args.config)
    app = get_app(config)

    @app.route('/upload/', methods=['POST'])
    def upload():

        if 'client' in config and 'user' in config[
                "client"] and 'pass' in config["client"]:
            auth_control = check_auth(config)
            if auth_control:
                return auth_control

        _file = request.files.get("file")
        if not _file:
            return "a file named as 'file' required", 400

        if not allowed_file(_file.filename, config):
            return "invalid file type", 400

        filename = secure_filename(_file.filename)
        full_filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)

        try:
            full_filename = get_available_filename(full_filename)
        except ValueError as error:
            return error.message, 400

        _file.save(full_filename)

        return os.path.split(full_filename)[1], 201

    app.debug = True

    with indent(2):
        puts("{} starting at {}:{}".format(colored.magenta("server"),
                                           config.get("HOST"),
                                           config.get("PORT")))
        puts("{} {}".format(colored.green("config"),
                            get_config_filename(args.config)))

    http_server = WSGIServer(('', int(config.get("PORT"))), app)
    http_server.serve_forever()
Beispiel #3
0
def main():

    args = get_cli_arguments()
    config = get_config(args.config)
    app = get_app(config)


    @app.route('/upload/', methods=['POST'])
    def upload():

        if 'client' in config and 'user' in config["client"] and 'pass' in config["client"]:
            auth_control = check_auth(config)
            if auth_control:
                return auth_control

        _file = request.files.get("file")
        if not _file:
            return "a file named as 'file' required", 400

        if not allowed_file(_file.filename, config):
            return "invalid file type", 400

        filename = secure_filename(_file.filename)
        full_filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)

        try:
            full_filename = get_available_filename(full_filename)
        except ValueError as error:
            return error.message, 400

        _file.save(full_filename)

        return os.path.split(full_filename)[1], 201

    app.debug = True

    with indent(2):
        puts("{} starting at {}:{}".format(colored.magenta("server"), config.get("HOST"), config.get("PORT")))
        puts("{} {}".format(colored.green("config"), get_config_filename(args.config)))

    http_server = WSGIServer(('', int(config.get("PORT"))), app)
    http_server.serve_forever()
Beispiel #4
0
def main():
    args = get_cli_arguments(for_client=True)
    config = get_config(args.config)

    if not os.path.exists(args.file):
        sys.exit("error: file doesn't exists: {}".format(args.file))

    if not 'client' in config:
        sys.exit("error: client section is empty on your config file.")

    request_parameters = {}

    if 'user' in config.get("client") and 'pass' in config.get("client"):
        request_parameters.update({
            "auth":
            requests.auth.HTTPBasicAuth(config["client"]["user"],
                                        config["client"]["pass"]),
        })

    with open(args.file, 'r') as payload:
        request_parameters.update({
            "files": {
                "file": payload
            },
        })
        try:
            r = requests.post(config["client"]["endpoint"],
                              **request_parameters)
        except requests.exceptions.ConnectionError:
            # @todo: how exactly requests library seperate requests with bad auth and requests with bad url?
            sys.exit("invalid URL or invalid credentials.")

        if r.status_code == 201:
            print "file successfully uploaded. {}{}".format(
                config.get("SERVE_URL"), r.content)
        else:
            sys.exit("error: {}".format(r.content))