-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpush.py
executable file
·160 lines (118 loc) · 3.95 KB
/
push.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
"""
push.py: example of how to push image to nginx upload server!
Copyright (C) 2018-2021 Vanessa Sochat.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import print_function
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
import requests
import argparse
import sys
import os
def get_parser():
parser = argparse.ArgumentParser(description="Dinosaur Nginx Upload Example")
description = "example push client to upload files to nginx upload endpoint"
parser.add_argument(
"--host",
dest="host",
help="the host where the server is running",
type=str,
default="127.0.0.1",
)
parser.add_argument(
"--port",
"-p",
dest="port",
help="the port where the server is running",
type=str,
default="",
)
parser.add_argument(
"--schema",
"-s",
dest="schema",
help="http:// or https://",
type=str,
default="http://",
)
parser.add_argument("file", nargs=1, help="full path to file to push", type=str)
return parser
def main():
"""the main entrypoint for pushing!"""
parser = get_parser()
# If the user didn't provide any arguments, show the full help
if len(sys.argv) == 1:
parser.print_help()
try:
args = parser.parse_args()
except:
sys.exit(0)
# Assemble host / port url
url = assemble_url(args.schema, args.host, args.port)
# Pass on to the correct parser
return_code = 0
try:
push(path=args.file[0], url=url)
sys.exit(return_code)
except UnboundLocalError:
return_code = 1
def assemble_url(schema, host, port):
if port:
port = ":%s" % port
return "%s%s%s/upload" % (schema, host, port)
def push(path, url):
"""
Push an image to the dinosaur nginx upload server!
Parameters
==========
path: the full path to the image.
"""
path = os.path.abspath(path)
image = os.path.basename(path)
if not os.path.exists(path):
print("ERROR: %s does not exist." % path)
sys.exit(1)
image_size = os.path.getsize(path) >> 20
print("PUSH %s of size %s" % (image, image_size))
upload_to = os.path.basename(path)
encoder = MultipartEncoder(
fields={
"name": upload_to,
"terminal": "yes",
"file1": (upload_to, open(path, "rb"), "text/plain"),
}
)
progress_callback = create_callback(encoder)
monitor = MultipartEncoderMonitor(encoder, progress_callback)
headers = {"Content-Type": monitor.content_type}
try:
r = requests.post(url, data=monitor, headers=headers)
message = r.json()["message"]
print("\n[Return status {0} {1}]".format(r.status_code, message))
except KeyboardInterrupt:
print("\nUpload cancelled.")
except Exception as e:
print(e)
sys.stdout.write("\n")
def create_callback(encoder):
encoder_len = int(encoder.len / (1024 * 1024.0))
sys.stdout.write("[0 of %s MB]" % (encoder_len))
sys.stdout.flush()
def callback(monitor):
sys.stdout.write("\r")
bytes_read = int(monitor.bytes_read / (1024 * 1024.0))
sys.stdout.write("[%s of %s MB]" % (bytes_read, encoder_len))
sys.stdout.flush()
return callback
if __name__ == "__main__":
main()