forked from lemariva/ESP32MicroPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp.py
executable file
·321 lines (291 loc) · 13.8 KB
/
ftp.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""
Small ftp server for ESP8266 and ESP32 Micropython
Based on the work of chrisgp - Christopher Popp and pfalcon - Paul Sokolovsky
The server accepts passive mode only.
It runs in a separated thread
Start the server with:
from ftp import ftpserver
u = ftpserver()
u.start_thread()
Distributed under MIT License
Copyright (c) 2016 Christopher Popp (initial ftp server framework)
Copyright (c) 2016 Robert Hammelrath (putting the pieces together and a few extensions)
Copyright (c) 2017 Mauro Riva (lemariva.com) (ftp class, dbg option and threading (only ESP32))
Copyright (c) 2018 Gautier HUSSON (liberasys.com) (reentrancy)
"""
import socket
import network
import uos
import gc
try:
import _thread
thread_available = True
except:
thread_available = False
DEBUG = False
#DEBUG = True
def dbg(msg, msg_=""):
global DEBUG
if DEBUG:
print (str(msg) + str(msg_))
class ftpserver():
def send_list_data(self, path, dataclient, full):
try:
# whether path is a directory name
for fname in sorted(uos.listdir(path), key = str.lower):
dataclient.sendall(self.make_description(path, fname, full))
except:
# path may be a file name or pattern
pattern = path.split("/")[-1]
path = path[:-(len(pattern) + 1)]
if path == "": path = "/"
for fname in sorted(uos.listdir(path), key = str.lower):
if fncmp(fname, pattern) == True:
dataclient.sendall(self.make_description(path, fname, full))
def make_description(self, path, fname, full):
if full:
stat = uos.stat(self.get_absolute_path(path,fname))
file_permissions = "drwxr-xr-x" if (stat[0] & 0o170000 == 0o040000) else "-rw-r--r--"
file_size = stat[6]
description = "{} 1 owner group {:>10} Jan 1 2000 {}\r\n".format(file_permissions, file_size, fname)
else:
description = fname + "\r\n"
return description
def send_file_data(self, path, dataclient):
with open(path, "r") as file:
chunk = file.read(512)
while len(chunk) > 0:
dataclient.sendall(chunk)
chunk = file.read(512)
def save_file_data(self, path, dataclient):
with open(path, "w") as file:
chunk = dataclient.recv(512)
while len(chunk) > 0:
file.write(chunk)
chunk = dataclient.recv(512)
def get_absolute_path(self, cwd, payload):
# Just a few special cases "..", "." and ""
# If payload start's with /, set cwd to /
# and consider the remainder a relative path
if payload.startswith('/'):
cwd = "/"
for token in payload.split("/"):
if token == '..':
if cwd != '/':
cwd = '/'.join(cwd.split('/')[:-1])
if cwd == '':
cwd = '/'
elif token != '.' and token != '':
if cwd == '/':
cwd += token
else:
cwd = cwd + '/' + token
return cwd
# compare fname against pattern. Pattern may contain
# wildcards ? and *.
def fncmp(fname, pattern):
pi = 0
si = 0
while pi < len(pattern) and si < len(fname):
if (fname[si] == pattern[pi]) or (pattern[pi] == '?'):
si += 1
pi += 1
else:
if pattern[pi] == '*': # recurse
if (pi + 1) == len(pattern):
return True
while si < len(fname):
if fncmp(fname[si:], pattern[pi+1:]) == True:
return True
else:
si += 1
return False
else:
return False
if pi == len(pattern.rstrip("*")) and si == len(fname):
return True
else:
return False
def thread_ftp(self, arg):
self.start()
dbg('Thread ftp ended')
def start_thread(self):
if thread_available:
dbg("Starting ftp server on separated thread\n")
_thread.start_new_thread(self.thread_ftp, ('',))
else:
self.start()
def start(self):
DATA_PORT = 13333
self.ftpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.datasocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ftpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.datasocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.ftpsocket.bind(socket.getaddrinfo("0.0.0.0", 21)[0][4])
self.datasocket.bind(socket.getaddrinfo("0.0.0.0", DATA_PORT)[0][4])
self.ftpsocket.listen(1)
self.ftpsocket.settimeout(None)
self.datasocket.listen(1)
self.datasocket.settimeout(None)
msg_250_OK = '250 OK\r\n'
msg_550_fail = '550 Failed\r\n'
self.wlan = network.WLAN(network.AP_IF)
if self.wlan.active():
ifconfig = self.wlan.ifconfig()
else:
self.wlan = network.WLAN(network.STA_IF)
if self.wlan.active():
ifconfig = self.wlan.ifconfig()
else:
dbg("No active connection")
return
addr = ifconfig[0]
print("FTP Server started on ", addr)
try:
while True:
dataclient = None
fromname = None
do_run = True
while do_run:
cl, remote_addr = self.ftpsocket.accept()
cl.settimeout(300)
cwd = '/'
try:
dbg("FTP connection from:", remote_addr)
cl.sendall("220 Hello, this is the " + uos.uname()[4] + ".\r\n")
while True:
data = cl.readline().decode("utf-8").rstrip("\r\n")
if len(data) <= 0:
dbg("Client disappeared")
do_run = False
break
command = data.split(" ")[0].upper()
payload = data[len(command):].lstrip()
path = self.get_absolute_path(cwd, payload)
dbg("Command={}, Payload={}".format(command, payload))
if command == "USER":
cl.sendall("230 Logged in.\r\n")
elif command == "SYST":
cl.sendall("215 UNIX Type: L8\r\n")
elif command == "NOOP":
cl.sendall("200 OK\r\n")
elif command == "FEAT":
cl.sendall("211 no-features\r\n")
elif command == "PWD":
cl.sendall('257 "{}"\r\n'.format(cwd))
elif command == "CWD":
try:
files = uos.listdir(path)
cwd = path
cl.sendall(msg_250_OK)
except:
cl.sendall(msg_550_fail)
elif command == "CDUP":
cwd = self.get_absolute_path(cwd, "..")
cl.sendall(msg_250_OK)
elif command == "TYPE":
# probably should switch between binary and not
cl.sendall('200 Transfer mode set\r\n')
elif command == "SIZE":
try:
size = uos.stat(path)[6]
cl.sendall('213 {}\r\n'.format(size))
except:
cl.sendall(msg_550_fail)
elif command == "QUIT":
cl.sendall('221 Bye.\r\n')
do_run = False
break
elif command == "PASV":
cl.sendall('227 Entering Passive Mode ({},{},{}).\r\n'.format(
addr.replace('.',','), DATA_PORT>>8, DATA_PORT%256))
dataclient, data_addr = self.datasocket.accept()
dbg("FTP Data connection from:", data_addr)
elif command == "LIST" or command == "NLST":
if not payload.startswith("-"):
place = path
else:
place = cwd
try:
self.send_list_data(place, dataclient, command == "LIST" or payload == "-l")
cl.sendall("150 Here comes the directory listing.\r\n")
cl.sendall("226 Listed.\r\n")
except:
cl.sendall(msg_550_fail)
if dataclient is not None:
dataclient.close()
dataclient = None
elif command == "RETR":
try:
self.send_file_data(path, dataclient)
cl.sendall("150 Opening data connection.\r\n")
cl.sendall("226 Transfer complete.\r\n")
except:
cl.sendall(msg_550_fail)
if dataclient is not None:
dataclient.close()
dataclient = None
elif command == "STOR":
try:
cl.sendall("150 Ok to send data.\r\n")
self.save_file_data(path, dataclient)
cl.sendall("226 Transfer complete.\r\n")
except:
cl.sendall(msg_550_fail)
if dataclient is not None:
dataclient.close()
dataclient = None
elif command == "DELE":
try:
uos.remove(path)
cl.sendall(msg_250_OK)
except:
cl.sendall(msg_550_fail)
elif command == "RMD":
try:
uos.rmdir(path)
cl.sendall(msg_250_OK)
except:
cl.sendall(msg_550_fail)
elif command == "MKD":
try:
uos.mkdir(path)
cl.sendall(msg_250_OK)
except:
cl.sendall(msg_550_fail)
elif command == "RNFR":
fromname = path
cl.sendall("350 Rename from\r\n")
elif command == "RNTO":
if fromname is not None:
try:
uos.rename(fromname, path)
cl.sendall(msg_250_OK)
except:
cl.sendall(msg_550_fail)
else:
cl.sendall(msg_550_fail)
fromname = None
elif command == "STAT":
if payload == "":
cl.sendall("211-Connected to ({})\r\n"
" Data address ({})\r\n"
"211 TYPE: Binary STRU: File MODE: Stream\r\n".format(
remote_addr[0], addr))
else:
cl.sendall("213-Directory listing:\r\n")
self.send_list_data(path, cl, True)
cl.sendall("213 Done.\r\n")
else:
cl.sendall("502 Unsupported command.\r\n")
dbg("Unsupported command {} with payload {}".format(command, payload))
except Exception as err:
dbg(err)
finally:
cl.close()
cl = None
if dataclient is not None:
dataclient.close()
finally:
self.datasocket.close()
self.ftpsocket.close()