-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_file.py
51 lines (35 loc) · 1.23 KB
/
upload_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
"""
Upload files via http.
"""
import sys
import argparse
import requests
PROGRAM_VERSION = '0.1.0'
def main(argv=None):
"Main function."
program_description = __doc__
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(
description=program_description, epilog=None,
formatter_class=argparse.RawDescriptionHelpFormatter, add_help=False)
parser.add_argument('--version', action='version',
version=PROGRAM_VERSION)
parser.add_argument('--help', action='help', default=argparse.SUPPRESS,
help="Show this help message and exit.")
parser.add_argument('uri', type=str,
metavar='<URI>',
help="""URI to upload.""")
parser.add_argument('filepath', type=str,
metavar='<FILE>',
help="""Path to the file.""")
args = parser.parse_args(argv)
with open(args.filepath, 'rb') as f:
resp = requests.post(args.uri, data=f)
if resp.status_code == 200:
print(resp.json())
else:
print(f"Error {resp.status_code}: {resp.reason}", file=sys.stderr)
if __name__ == '__main__':
main()