@@ -69,18 +69,21 @@ def update_progress(progress):
69
69
progress = 1
70
70
status = "Done...\r \n "
71
71
block = int (round (PROGRESS_BAR_LENGTH * progress ))
72
- text = "\r Uploading: [{0}] {1}% {2}" .format ("=" * block + " " * (PROGRESS_BAR_LENGTH - block ), int (progress * 100 ), status )
72
+ text = "\r Uploading: [{0}] {1}% {2}" .format (
73
+ "=" * block + " " * (PROGRESS_BAR_LENGTH - block ), int (progress * 100 ), status
74
+ )
73
75
sys .stderr .write (text )
74
76
sys .stderr .flush ()
75
77
else :
76
78
sys .stderr .write ("." )
77
79
sys .stderr .flush ()
78
80
81
+
79
82
def serve (remote_addr , local_addr , remote_port , local_port , password , filename , command = FLASH ):
80
83
# Create a TCP/IP socket
81
84
sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
82
85
server_address = (local_addr , local_port )
83
- logging .info (' Starting on %s:%s' , str (server_address [0 ]), str (server_address [1 ]))
86
+ logging .info (" Starting on %s:%s" , str (server_address [0 ]), str (server_address [1 ]))
84
87
try :
85
88
sock .bind (server_address )
86
89
sock .listen (1 )
@@ -89,14 +92,14 @@ def serve(remote_addr, local_addr, remote_port, local_port, password, filename,
89
92
return 1
90
93
91
94
content_size = os .path .getsize (filename )
92
- file_md5 = hashlib .md5 (open (filename , 'rb' ).read ()).hexdigest ()
93
- logging .info (' Upload size: %d' , content_size )
94
- message = ' %d %d %d %s\n ' % (command , local_port , content_size , file_md5 )
95
+ file_md5 = hashlib .md5 (open (filename , "rb" ).read ()).hexdigest ()
96
+ logging .info (" Upload size: %d" , content_size )
97
+ message = " %d %d %d %s\n " % (command , local_port , content_size , file_md5 )
95
98
96
99
# Wait for a connection
97
100
inv_tries = 0
98
- data = ''
99
- msg = ' Sending invitation to %s ' % remote_addr
101
+ data = ""
102
+ msg = " Sending invitation to %s " % remote_addr
100
103
sys .stderr .write (msg )
101
104
sys .stderr .flush ()
102
105
while inv_tries < 10 :
@@ -106,73 +109,73 @@ def serve(remote_addr, local_addr, remote_port, local_port, password, filename,
106
109
try :
107
110
sent = sock2 .sendto (message .encode (), remote_address )
108
111
except :
109
- sys .stderr .write (' failed\n ' )
112
+ sys .stderr .write (" failed\n " )
110
113
sys .stderr .flush ()
111
114
sock2 .close ()
112
- logging .error (' Host %s Not Found' , remote_addr )
115
+ logging .error (" Host %s Not Found" , remote_addr )
113
116
return 1
114
117
sock2 .settimeout (TIMEOUT )
115
118
try :
116
119
data = sock2 .recv (37 ).decode ()
117
120
break
118
121
except :
119
- sys .stderr .write ('.' )
122
+ sys .stderr .write ("." )
120
123
sys .stderr .flush ()
121
124
sock2 .close ()
122
- sys .stderr .write (' \n ' )
125
+ sys .stderr .write (" \n " )
123
126
sys .stderr .flush ()
124
127
if inv_tries == 10 :
125
- logging .error (' No response from the ESP' )
128
+ logging .error (" No response from the ESP" )
126
129
return 1
127
130
if data != "OK" :
128
- if data .startswith (' AUTH' ):
131
+ if data .startswith (" AUTH" ):
129
132
nonce = data .split ()[1 ]
130
- cnonce_text = ' %s%u%s%s' % (filename , content_size , file_md5 , remote_addr )
133
+ cnonce_text = " %s%u%s%s" % (filename , content_size , file_md5 , remote_addr )
131
134
cnonce = hashlib .md5 (cnonce_text .encode ()).hexdigest ()
132
135
passmd5 = hashlib .md5 (password .encode ()).hexdigest ()
133
- result_text = ' %s:%s:%s' % (passmd5 , nonce , cnonce )
136
+ result_text = " %s:%s:%s" % (passmd5 , nonce , cnonce )
134
137
result = hashlib .md5 (result_text .encode ()).hexdigest ()
135
- sys .stderr .write (' Authenticating...' )
138
+ sys .stderr .write (" Authenticating..." )
136
139
sys .stderr .flush ()
137
- message = ' %d %s %s\n ' % (AUTH , cnonce , result )
140
+ message = " %d %s %s\n " % (AUTH , cnonce , result )
138
141
sock2 .sendto (message .encode (), remote_address )
139
142
sock2 .settimeout (10 )
140
143
try :
141
144
data = sock2 .recv (32 ).decode ()
142
145
except :
143
- sys .stderr .write (' FAIL\n ' )
144
- logging .error (' No Answer to our Authentication' )
146
+ sys .stderr .write (" FAIL\n " )
147
+ logging .error (" No Answer to our Authentication" )
145
148
sock2 .close ()
146
149
return 1
147
150
if data != "OK" :
148
- sys .stderr .write (' FAIL\n ' )
149
- logging .error ('%s' , data )
151
+ sys .stderr .write (" FAIL\n " )
152
+ logging .error ("%s" , data )
150
153
sock2 .close ()
151
154
sys .exit (1 )
152
155
return 1
153
- sys .stderr .write (' OK\n ' )
156
+ sys .stderr .write (" OK\n " )
154
157
else :
155
- logging .error (' Bad Answer: %s' , data )
158
+ logging .error (" Bad Answer: %s" , data )
156
159
sock2 .close ()
157
160
return 1
158
161
sock2 .close ()
159
162
160
- logging .info (' Waiting for device...' )
163
+ logging .info (" Waiting for device..." )
161
164
try :
162
165
sock .settimeout (10 )
163
166
connection , client_address = sock .accept ()
164
167
sock .settimeout (None )
165
168
connection .settimeout (None )
166
169
except :
167
- logging .error (' No response from device' )
170
+ logging .error (" No response from device" )
168
171
sock .close ()
169
172
return 1
170
173
try :
171
174
with open (filename , "rb" ) as f :
172
175
if PROGRESS :
173
176
update_progress (0 )
174
177
else :
175
- sys .stderr .write (' Uploading' )
178
+ sys .stderr .write (" Uploading" )
176
179
sys .stderr .flush ()
177
180
offset = 0
178
181
while True :
@@ -185,39 +188,39 @@ def serve(remote_addr, local_addr, remote_port, local_port, password, filename,
185
188
try :
186
189
connection .sendall (chunk )
187
190
res = connection .recv (10 )
188
- last_response_contained_ok = 'OK' in res .decode ()
191
+ last_response_contained_ok = "OK" in res .decode ()
189
192
except Exception as e :
190
- sys .stderr .write (' \n ' )
191
- logging .error (' Error Uploading: %s' , str (e ))
193
+ sys .stderr .write (" \n " )
194
+ logging .error (" Error Uploading: %s" , str (e ))
192
195
connection .close ()
193
196
return 1
194
197
195
198
if last_response_contained_ok :
196
- logging .info (' Success' )
199
+ logging .info (" Success" )
197
200
connection .close ()
198
201
return 0
199
202
200
- sys .stderr .write (' \n ' )
201
- logging .info (' Waiting for result...' )
203
+ sys .stderr .write (" \n " )
204
+ logging .info (" Waiting for result..." )
202
205
count = 0
203
206
while count < 5 :
204
207
count += 1
205
208
connection .settimeout (60 )
206
209
try :
207
210
data = connection .recv (32 ).decode ()
208
- logging .info (' Result: %s' , data )
211
+ logging .info (" Result: %s" , data )
209
212
210
213
if "OK" in data :
211
- logging .info (' Success' )
214
+ logging .info (" Success" )
212
215
connection .close ()
213
216
return 0
214
217
215
218
except Exception as e :
216
- logging .error (' Error receiving result: %s' , str (e ))
219
+ logging .error (" Error receiving result: %s" , str (e ))
217
220
connection .close ()
218
221
return 1
219
222
220
- logging .error (' Error response from device' )
223
+ logging .error (" Error response from device" )
221
224
connection .close ()
222
225
return 1
223
226
@@ -229,29 +232,60 @@ def serve(remote_addr, local_addr, remote_port, local_port, password, filename,
229
232
230
233
231
234
def parse_args (unparsed_args ):
232
- parser = argparse .ArgumentParser (
233
- description = "Transmit image over the air to the ESP32 module with OTA support."
234
- )
235
+ parser = argparse .ArgumentParser (description = "Transmit image over the air to the ESP32 module with OTA support." )
235
236
236
237
# destination ip and port
237
238
parser .add_argument ("-i" , "--ip" , dest = "esp_ip" , action = "store" , help = "ESP32 IP Address." , default = False )
238
239
parser .add_argument ("-I" , "--host_ip" , dest = "host_ip" , action = "store" , help = "Host IP Address." , default = "0.0.0.0" )
239
240
parser .add_argument ("-p" , "--port" , dest = "esp_port" , type = int , help = "ESP32 OTA Port. Default: 3232" , default = 3232 )
240
241
parser .add_argument (
241
- "-P" , "--host_port" , dest = "host_port" , type = int , help = "Host server OTA Port. Default: random 10000-60000" , default = random .randint (10000 , 60000 )
242
+ "-P" ,
243
+ "--host_port" ,
244
+ dest = "host_port" ,
245
+ type = int ,
246
+ help = "Host server OTA Port. Default: random 10000-60000" ,
247
+ default = random .randint (10000 , 60000 ),
242
248
)
243
249
244
250
# authentication
245
251
parser .add_argument ("-a" , "--auth" , dest = "auth" , help = "Set authentication password." , action = "store" , default = "" )
246
252
247
253
# image
248
254
parser .add_argument ("-f" , "--file" , dest = "image" , help = "Image file." , metavar = "FILE" , default = None )
249
- parser .add_argument ("-s" , "--spiffs" , dest = "spiffs" , action = "store_true" , help = "Transmit a SPIFFS image and do not flash the module." , default = False )
255
+ parser .add_argument (
256
+ "-s" ,
257
+ "--spiffs" ,
258
+ dest = "spiffs" ,
259
+ action = "store_true" ,
260
+ help = "Transmit a SPIFFS image and do not flash the module." ,
261
+ default = False ,
262
+ )
250
263
251
264
# output
252
- parser .add_argument ("-d" , "--debug" , dest = "debug" , action = "store_true" , help = "Show debug output. Overrides loglevel with debug." , default = False )
253
- parser .add_argument ("-r" , "--progress" , dest = "progress" , action = "store_true" , help = "Show progress output. Does not work for Arduino IDE." , default = False )
254
- parser .add_argument ("-t" , "--timeout" , dest = "timeout" , type = int , help = "Timeout to wait for the ESP32 to accept invitation." , default = 10 )
265
+ parser .add_argument (
266
+ "-d" ,
267
+ "--debug" ,
268
+ dest = "debug" ,
269
+ action = "store_true" ,
270
+ help = "Show debug output. Overrides loglevel with debug." ,
271
+ default = False ,
272
+ )
273
+ parser .add_argument (
274
+ "-r" ,
275
+ "--progress" ,
276
+ dest = "progress" ,
277
+ action = "store_true" ,
278
+ help = "Show progress output. Does not work for Arduino IDE." ,
279
+ default = False ,
280
+ )
281
+ parser .add_argument (
282
+ "-t" ,
283
+ "--timeout" ,
284
+ dest = "timeout" ,
285
+ type = int ,
286
+ help = "Timeout to wait for the ESP32 to accept invitation." ,
287
+ default = 10 ,
288
+ )
255
289
256
290
return parser .parse_args (unparsed_args )
257
291
0 commit comments