好久没出教程了,因为我秉承只上干货的原则,只发有用的干货(偶尔也夹带一些自己的碎碎念),然后就是能上代码直接粘贴出来,让大家来了就不白来,不要随便浪费别人的时间。
不废话了,这次用micropython做了一个ftp的服务端,github上面找来的,很精简,就一个py文件,需要的拿走,直接贴代码。
#vx jd3096
import socket
import network
import uos
import gc
from time import localtime
SSID='findx3'
PASSWORD='800080008000'
wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID,PASSWORD)
if wlan.isconnected():
print('WIFI connected already!')
else:
while not wlan.isconnected():
pass
print('WIFI connected first')
print(wlan.ifconfig()[0]) #建立WIFI连接的方法自己随便弄,这里给个参考,ftp的地址就是打印出来的,ifconfig()的第一项
month_name = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
def send_list_data(path, dataclient, full):
try: # whether path is a directory name
for fname in uos.listdir(path):
dataclient.sendall(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 uos.listdir(path):
if fncmp(fname, pattern) == True:
dataclient.sendall(make_description(path, fname, full))
def make_description(path, fname, full):
if full:
stat = uos.stat(get_absolute_path(path,fname))
file_permissions = "drwxr-xr-x" if (stat[0] & 0o170000 == 0o040000) else "-rw-r--r--"
file_size = stat[6]
tm = localtime(stat[7])
if tm[0] != localtime()[0]:
description = "{} 1 owner group {:>10} {} {:2} {:>5} {}\r\n".format(
file_permissions, file_size, month_name[tm[1]], tm[2], tm[0], fname)
else:
description = "{} 1 owner group {:>10} {} {:2} {:02}:{:02} {}\r\n".format(
file_permissions, file_size, month_name[tm[1]], tm[2], tm[3], tm[4], fname)
else:
description = fname + "\r\n"
return description
def send_file_data(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(path, dataclient, mode):
with open(path, mode) as file:
chunk = dataclient.read(512)
while len(chunk) > 0:
file.wri

博主分享了一篇教程,利用MicroPython实现了一个简单的FTP服务器,仅需一个Python文件。教程详细介绍了代码实现过程,包括连接WiFi、处理FTP命令如LIST、RETR、STOR等。注意,最新版的EDGE和Chrome浏览器已不支持FTP。
最低0.47元/天 解锁文章
1323





