micropython建立ftp(服务端)含源码

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

好久没出教程了,因为我秉承只上干货的原则,只发有用的干货(偶尔也夹带一些自己的碎碎念),然后就是能上代码直接粘贴出来,让大家来了就不白来,不要随便浪费别人的时间。
不废话了,这次用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
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值