11#!/usr/bin/env python
22
3- '''
3+ """
44
55push.py: example of how to push image to nginx upload server!
66
7- Copyright (C) 2018 Vanessa Sochat.
7+ Copyright (C) 2018-2021 Vanessa Sochat.
88
99This program is free software: you can redistribute it and/or modify it
1010under the terms of the GNU Affero General Public License as published by
1919You should have received a copy of the GNU Affero General Public License
2020along with this program. If not, see <https://www.gnu.org/licenses/>.
2121
22- '''
22+ """
2323
2424from __future__ import print_function
25- from requests_toolbelt .streaming_iterator import StreamingIterator
26- from requests_toolbelt import (
27- MultipartEncoder ,
28- MultipartEncoderMonitor
29- )
25+ from requests_toolbelt import MultipartEncoder , MultipartEncoderMonitor
3026
3127import requests
3228import argparse
33- import hashlib
3429import sys
3530import os
3631
3732
3833def get_parser ():
3934 parser = argparse .ArgumentParser (description = "Dinosaur Nginx Upload Example" )
4035
41- description = 'example push client to upload files to nginx upload endpoint'
42-
43- parser .add_argument ("--host" , dest = 'host' ,
44- help = "the host where the server is running" ,
45- type = str , default = '127.0.0.1' )
46-
47- parser .add_argument ("--port" , "-p" , dest = 'port' ,
48- help = "the port where the server is running" ,
49- type = str , default = '' )
50-
51- parser .add_argument ("--schema" , "-s" , dest = 'schema' ,
52- help = "http:// or https://" ,
53- type = str , default = 'http://' )
54-
55- parser .add_argument ("file" , nargs = 1 ,
56- help = "full path to file to push" ,
57- type = str )
36+ description = "example push client to upload files to nginx upload endpoint"
37+
38+ parser .add_argument (
39+ "--host" ,
40+ dest = "host" ,
41+ help = "the host where the server is running" ,
42+ type = str ,
43+ default = "127.0.0.1" ,
44+ )
45+
46+ parser .add_argument (
47+ "--port" ,
48+ "-p" ,
49+ dest = "port" ,
50+ help = "the port where the server is running" ,
51+ type = str ,
52+ default = "" ,
53+ )
54+
55+ parser .add_argument (
56+ "--schema" ,
57+ "-s" ,
58+ dest = "schema" ,
59+ help = "http:// or https://" ,
60+ type = str ,
61+ default = "http://" ,
62+ )
63+
64+ parser .add_argument ("file" , nargs = 1 , help = "full path to file to push" , type = str )
5865
5966 return parser
6067
6168
62-
6369def main ():
64- '''the main entrypoint for pushing!
65- '''
70+ """the main entrypoint for pushing!"""
6671
6772 parser = get_parser ()
6873
@@ -88,62 +93,68 @@ def main():
8893
8994def assemble_url (schema , host , port ):
9095 if port :
91- port = ' :%s' % port
92- return ' %s%s%s/upload' % (schema , host , port )
96+ port = " :%s" % port
97+ return " %s%s%s/upload" % (schema , host , port )
9398
9499
95100def push (path , url ):
96- '''push an image to the dinosaur nginx upload server!
97-
98- Parameters
99- ==========
100- path: the full path to the image.
101+ """
102+ Push an image to the dinosaur nginx upload server!
101103
102- '''
104+ Parameters
105+ ==========
106+ path: the full path to the image.
107+ """
103108
104109 path = os .path .abspath (path )
105110 image = os .path .basename (path )
106- print ("PUSH %s" % path )
107111
108112 if not os .path .exists (path ):
109- print (' ERROR: %s does not exist.' % path )
113+ print (" ERROR: %s does not exist." % path )
110114 sys .exit (1 )
111115
112116 image_size = os .path .getsize (path ) >> 20
117+ print ("PUSH %s of size %s" % (image , image_size ))
113118
114119 upload_to = os .path .basename (path )
115120
116- encoder = MultipartEncoder (fields = {'name' : upload_to ,
117- 'terminal' : "yes" ,
118- 'file1' : (upload_to , open (path , 'rb' ), 'text/plain' )})
121+ encoder = MultipartEncoder (
122+ fields = {
123+ "name" : upload_to ,
124+ "terminal" : "yes" ,
125+ "file1" : (upload_to , open (path , "rb" ), "text/plain" ),
126+ }
127+ )
119128
120129 progress_callback = create_callback (encoder )
121130 monitor = MultipartEncoderMonitor (encoder , progress_callback )
122- headers = {' Content-Type' : monitor .content_type }
131+ headers = {" Content-Type" : monitor .content_type }
123132
124133 try :
125134 r = requests .post (url , data = monitor , headers = headers )
126- message = r .json ()[' message' ]
127- print (' \n [Return status {0} {1}]' .format (r .status_code , message ))
135+ message = r .json ()[" message" ]
136+ print (" \n [Return status {0} {1}]" .format (r .status_code , message ))
128137 except KeyboardInterrupt :
129- print (' \n Upload cancelled.' )
138+ print (" \n Upload cancelled." )
130139 except Exception as e :
131140 print (e )
132141
133142 sys .stdout .write ("\n " )
134143
135144
136145def create_callback (encoder ):
137- encoder_len = int (encoder .len / (1024 * 1024.0 ))
146+ encoder_len = int (encoder .len / (1024 * 1024.0 ))
138147 sys .stdout .write ("[0 of %s MB]" % (encoder_len ))
139148 sys .stdout .flush ()
149+
140150 def callback (monitor ):
141- sys .stdout .write (' \r ' )
142- bytes_read = int (monitor .bytes_read / (1024 * 1024.0 ))
151+ sys .stdout .write (" \r " )
152+ bytes_read = int (monitor .bytes_read / (1024 * 1024.0 ))
143153 sys .stdout .write ("[%s of %s MB]" % (bytes_read , encoder_len ))
144154 sys .stdout .flush ()
155+
145156 return callback
146157
147158
148- if __name__ == ' __main__' :
159+ if __name__ == " __main__" :
149160 main ()
0 commit comments