summaryrefslogtreecommitdiffstats
path: root/test/tools/http_put.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 21:30:40 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 21:30:40 +0000
commit133a45c109da5310add55824db21af5239951f93 (patch)
treeba6ac4c0a950a0dda56451944315d66409923918 /test/tools/http_put.py
parentInitial commit. (diff)
downloadrspamd-133a45c109da5310add55824db21af5239951f93.tar.xz
rspamd-133a45c109da5310add55824db21af5239951f93.zip
Adding upstream version 3.8.1.upstream/3.8.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/tools/http_put.py')
-rwxr-xr-xtest/tools/http_put.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/test/tools/http_put.py b/test/tools/http_put.py
new file mode 100755
index 0000000..8ede68e
--- /dev/null
+++ b/test/tools/http_put.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+"""
+Small script to upload file using HTTP PUT
+"""
+
+import argparse
+import os
+import sys
+
+import requests
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description='Upload a file using HTTP PUT method',
+ epilog=(
+ "To use HTTP Auth set HTTP_PUT_AUTH environment variable to user:password\n"
+ "Example: %(prog)s file1 file2 https://example.com/dir/"))
+ parser.add_argument(
+ "file", type=argparse.FileType('rb'), nargs='+', help="File to upload")
+ parser.add_argument(
+ "dir_url", help="Remote URL (path to a directory, must include a trailing /)")
+ args = parser.parse_args()
+
+ if not args.dir_url.endswith('/'):
+ parser.error("URL must end with /")
+
+ http_auth = os.getenv('HTTP_PUT_AUTH')
+ if http_auth:
+ user, password = http_auth.split(':')
+ auth = (user, password)
+ else:
+ auth = None
+
+ exit_code = 0
+
+ for fh in args.file:
+ try:
+ r = requests.put(args.dir_url + fh.name, data=fh, auth=auth, timeout=(45, 90))
+ r.raise_for_status()
+ print("{} uploaded to {}".format(fh.name, r.url))
+ except (requests.exceptions.ConnectionError,
+ requests.exceptions.HTTPError) as err:
+ print(err, file=sys.stderr)
+ exit_code = 1
+
+ return exit_code
+
+
+if __name__ == '__main__':
+ sys.exit(main())