3030import optparse
3131import logging
3232import hashlib
33+ import random
3334
3435# Commands
3536FLASH = 0
3637SPIFFS = 100
3738AUTH = 200
38- PROGRESS = 0
39+ PROGRESS = False
3940# update_progress() : Displays or updates a console progress bar
4041## Accepts a float between 0 and 1. Any int will be converted to a float.
4142## A value under 0 represents a 'halt'.
4243## A value at 1 or bigger represents 100%
4344def update_progress (progress ):
44- if (PROGRESS == 1 ):
45+ if (PROGRESS ):
4546 barLength = 60 # Modify this to change the length of the progress bar
4647 status = ""
4748 if isinstance (progress , int ):
@@ -66,7 +67,7 @@ def update_progress(progress):
6667def serve (remoteAddr , remotePort , password , filename , command = FLASH ):
6768 # Create a TCP/IP socket
6869 sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
69- serverPort = 48266
70+ serverPort = random . randint ( 10000 , 60000 )
7071 server_address = ('0.0.0.0' , serverPort )
7172 logging .info ('Starting on %s:%s' , str (server_address [0 ]), str (server_address [1 ]))
7273 try :
@@ -141,11 +142,11 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
141142
142143 try :
143144 f = open (filename , "rb" )
144- if (PROGRESS == 0 ):
145+ if (PROGRESS ):
146+ update_progress (0 )
147+ else :
145148 sys .stderr .write ('Uploading' )
146149 sys .stderr .flush ()
147- else :
148- update_progress (0 )
149150 offset = 0
150151 while True :
151152 chunk = f .read (1460 )
@@ -195,108 +196,108 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
195196
196197
197198def parser ():
198- parser = optparse .OptionParser (
199- usage = "%prog [options]" ,
200- description = "Transmit image over the air to the esp8266 module with OTA support."
201- )
199+ parser = optparse .OptionParser (
200+ usage = "%prog [options]" ,
201+ description = "Transmit image over the air to the esp8266 module with OTA support."
202+ )
202203
203- # destination ip and port
204- group = optparse .OptionGroup (parser , "Destination" )
205- group .add_option ("-i" , "--ip" ,
206- dest = "esp_ip" ,
207- action = "store" ,
208- help = "ESP8266 IP Address." ,
209- default = False
210- )
211- group .add_option ("-p" , "--port" ,
212- dest = "esp_port" ,
213- type = "int" ,
214- help = "ESP8266 ota Port. Default 8266" ,
215- default = 8266
216- )
217- parser .add_option_group (group )
204+ # destination ip and port
205+ group = optparse .OptionGroup (parser , "Destination" )
206+ group .add_option ("-i" , "--ip" ,
207+ dest = "esp_ip" ,
208+ action = "store" ,
209+ help = "ESP8266 IP Address." ,
210+ default = False
211+ )
212+ group .add_option ("-p" , "--port" ,
213+ dest = "esp_port" ,
214+ type = "int" ,
215+ help = "ESP8266 ota Port. Default 8266" ,
216+ default = 8266
217+ )
218+ parser .add_option_group (group )
218219
219- # auth
220- group = optparse .OptionGroup (parser , "Authentication" )
221- group .add_option ("-a" , "--auth" ,
222- dest = "auth" ,
223- help = "Set authentication password." ,
224- action = "store" ,
225- default = ""
226- )
227- parser .add_option_group (group )
220+ # auth
221+ group = optparse .OptionGroup (parser , "Authentication" )
222+ group .add_option ("-a" , "--auth" ,
223+ dest = "auth" ,
224+ help = "Set authentication password." ,
225+ action = "store" ,
226+ default = ""
227+ )
228+ parser .add_option_group (group )
228229
229- # image
230- group = optparse .OptionGroup (parser , "Image" )
231- group .add_option ("-f" , "--file" ,
232- dest = "image" ,
233- help = "Image file." ,
234- metavar = "FILE" ,
235- default = None
236- )
237- group .add_option ("-s" , "--spiffs" ,
238- dest = "spiffs" ,
239- action = "store_true" ,
240- help = "Use this option to transmit a SPIFFS image and do not flash the module." ,
241- default = False
242- )
243- parser .add_option_group (group )
230+ # image
231+ group = optparse .OptionGroup (parser , "Image" )
232+ group .add_option ("-f" , "--file" ,
233+ dest = "image" ,
234+ help = "Image file." ,
235+ metavar = "FILE" ,
236+ default = None
237+ )
238+ group .add_option ("-s" , "--spiffs" ,
239+ dest = "spiffs" ,
240+ action = "store_true" ,
241+ help = "Use this option to transmit a SPIFFS image and do not flash the module." ,
242+ default = False
243+ )
244+ parser .add_option_group (group )
244245
245- # output group
246- group = optparse .OptionGroup (parser , "Output" )
247- group .add_option ("-d" , "--debug" ,
248- dest = "debug" ,
249- help = "Show debug output. And override loglevel with debug." ,
250- action = "store_true" ,
251- default = False
252- )
253- group .add_option ("-r" , "--progress" ,
254- dest = "progress" ,
255- help = "Show progress output. Does not work for ArduinoIDE" ,
256- action = "store_true" ,
257- default = False
258- )
259- parser .add_option_group (group )
246+ # output group
247+ group = optparse .OptionGroup (parser , "Output" )
248+ group .add_option ("-d" , "--debug" ,
249+ dest = "debug" ,
250+ help = "Show debug output. And override loglevel with debug." ,
251+ action = "store_true" ,
252+ default = False
253+ )
254+ group .add_option ("-r" , "--progress" ,
255+ dest = "progress" ,
256+ help = "Show progress output. Does not work for ArduinoIDE" ,
257+ action = "store_true" ,
258+ default = False
259+ )
260+ parser .add_option_group (group )
260261
261- (options , args ) = parser .parse_args ()
262+ (options , args ) = parser .parse_args ()
262263
263- return options
264+ return options
264265# end parser
265266
266267
267268def main (args ):
268- # get options
269- options = parser ()
269+ # get options
270+ options = parser ()
270271
271- # adapt log level
272- loglevel = logging .WARNING
273- if (options .debug ):
274- loglevel = logging .DEBUG
275- # end if
272+ # adapt log level
273+ loglevel = logging .WARNING
274+ if (options .debug ):
275+ loglevel = logging .DEBUG
276+ # end if
276277
277- # logging
278- logging .basicConfig (level = loglevel , format = '%(asctime)-8s [%(levelname)s]: %(message)s' , datefmt = '%H:%M:%S' )
278+ # logging
279+ logging .basicConfig (level = loglevel , format = '%(asctime)-8s [%(levelname)s]: %(message)s' , datefmt = '%H:%M:%S' )
279280
280- logging .debug ("Options: %s" , str (options ))
281+ logging .debug ("Options: %s" , str (options ))
281282
282- # check options
283- if ( options . progress ):
284- PROGRESS = 1
285- if (not options .esp_ip or not options .image ):
286- logging .critical ("Not enough arguments." )
283+ # check options
284+ global PROGRESS
285+ PROGRESS = options . progress
286+ if (not options .esp_ip or not options .image ):
287+ logging .critical ("Not enough arguments." )
287288
288- return 1
289- # end if
289+ return 1
290+ # end if
290291
291- command = FLASH
292- if (options .spiffs ):
293- command = SPIFFS
294- # end if
292+ command = FLASH
293+ if (options .spiffs ):
294+ command = SPIFFS
295+ # end if
295296
296- return serve (options .esp_ip , options .esp_port , options .auth , options .image , command )
297+ return serve (options .esp_ip , options .esp_port , options .auth , options .image , command )
297298# end main
298299
299300
300301if __name__ == '__main__' :
301- sys .exit (main (sys .argv ))
302+ sys .exit (main (sys .argv ))
302303# end if
0 commit comments