30
30
import optparse
31
31
import logging
32
32
import hashlib
33
+ import random
33
34
34
35
# Commands
35
36
FLASH = 0
36
37
SPIFFS = 100
37
38
AUTH = 200
38
- PROGRESS = 0
39
+ PROGRESS = False
39
40
# update_progress() : Displays or updates a console progress bar
40
41
## Accepts a float between 0 and 1. Any int will be converted to a float.
41
42
## A value under 0 represents a 'halt'.
42
43
## A value at 1 or bigger represents 100%
43
44
def update_progress (progress ):
44
- if (PROGRESS == 1 ):
45
+ if (PROGRESS ):
45
46
barLength = 60 # Modify this to change the length of the progress bar
46
47
status = ""
47
48
if isinstance (progress , int ):
@@ -66,7 +67,7 @@ def update_progress(progress):
66
67
def serve (remoteAddr , remotePort , password , filename , command = FLASH ):
67
68
# Create a TCP/IP socket
68
69
sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
69
- serverPort = 48266
70
+ serverPort = random . randint ( 10000 , 60000 )
70
71
server_address = ('0.0.0.0' , serverPort )
71
72
logging .info ('Starting on %s:%s' , str (server_address [0 ]), str (server_address [1 ]))
72
73
try :
@@ -141,11 +142,11 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
141
142
142
143
try :
143
144
f = open (filename , "rb" )
144
- if (PROGRESS == 0 ):
145
+ if (PROGRESS ):
146
+ update_progress (0 )
147
+ else :
145
148
sys .stderr .write ('Uploading' )
146
149
sys .stderr .flush ()
147
- else :
148
- update_progress (0 )
149
150
offset = 0
150
151
while True :
151
152
chunk = f .read (1460 )
@@ -195,108 +196,108 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
195
196
196
197
197
198
def 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
+ )
202
203
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 )
218
219
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 )
228
229
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 )
244
245
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 )
260
261
261
- (options , args ) = parser .parse_args ()
262
+ (options , args ) = parser .parse_args ()
262
263
263
- return options
264
+ return options
264
265
# end parser
265
266
266
267
267
268
def main (args ):
268
- # get options
269
- options = parser ()
269
+ # get options
270
+ options = parser ()
270
271
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
276
277
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' )
279
280
280
- logging .debug ("Options: %s" , str (options ))
281
+ logging .debug ("Options: %s" , str (options ))
281
282
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." )
287
288
288
- return 1
289
- # end if
289
+ return 1
290
+ # end if
290
291
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
295
296
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 )
297
298
# end main
298
299
299
300
300
301
if __name__ == '__main__' :
301
- sys .exit (main (sys .argv ))
302
+ sys .exit (main (sys .argv ))
302
303
# end if
0 commit comments