FTP Server

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

FTP Server

Post by chrisgp » Mon Oct 03, 2016 4:39 am

Hello,

I wanted a more convenient (for me) way of managing files on my device so I implemented a prototype minimal FTP server that can run on the ESP8266. It needs more work before it is generally useful, but it's hacked together enough to handle LS, GET and PUT commands for demo purposes: https://github.com/cpopp/MicroFTPServer

Here's a screenshot showing a file listing in chrome and a command line ftp client retrieving a file

Image

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: FTP Server

Post by Roberthh » Mon Oct 03, 2016 6:50 am

Hello @chrisgp. That is a VERY useful script. With background operation, it will be the perfect support for file transfer, when a web console is not needed.
Tested on a Wemos D1 device, both in RAM and frozen bytecode, with command line ftp. FileZilla shows the directory, but file transfer fails on ECONNRESET.
With command line ftp, ls and get work, but put tellls if has sent xxx bytes, but the stored file is just 128 bytes long. Fixed it by

Code: Select all

def save_file_data(path, dataclient):
    with open(path, "w") as file:
        chunk = dataclient.read(128)
        while len(chunk) > 0:
            file.write(chunk)
            chunk = dataclient.read(128)


User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: FTP Server

Post by Roberthh » Mon Oct 03, 2016 7:58 am

I made two small changes for my convenience, maybe only temporary:
- put the main code into a function, e.g. ftpserver()
- terminate the server when the client quits
That allows to change between REPL prompt and file transfer w/o rebooting the ESP8266.
I have placed the changed code here: https://github.com/robert-hh/Shared-Stuff
I know it's not git style, but this is easier than going through the PR pain.
Update: Looking at the behavior of Chrome, terminating the server if the client quits seems not appropriate, since Chrome send a quit after each dir-list, file display, ....

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: FTP Server

Post by chrisgp » Tue Oct 04, 2016 5:03 am

Glad to hear you tried it out and thanks for catching the bug saving files -- I committed that fix to my repository.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: FTP Server

Post by Roberthh » Tue Oct 04, 2016 5:49 am

I've added also the actual date/time values to the LIST command output, and tried to implement mkdir/rmdir/delete. The latter is not yet in my file space on the web. Sometimes the ESP crashes (reboot) when I run these commands. Not sure why.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: FTP Server

Post by Roberthh » Tue Oct 04, 2016 4:18 pm

I just tested FireFTP extension to Firefox, which works nice, including copying, opening, editing & saving on spot. Needs a support for the NOOP command. I added it, and also support for delete, mkdir and rename. Like yesterday, I've placed that version in this spot: https://github.com/robert-hh/Shared-Stuff. Sometimes, it still blocks, but I cannot tell why.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: FTP Server

Post by Roberthh » Fri Oct 07, 2016 6:59 pm

Hello @chrisgp. I made a few more additions to your uftpsever.py (sorry for hijacking it), wasting some time on spurious crashes, which turned out to be causes by an outdated mpy-cross. It was tested on various platforms & software pieces.
Linux:
  • ftp: works for file & directory operations w/o support for the m* commands
  • nautilus, thunar, filezilla, fireftp: work fine, including loading into the editor & saving back
  • Chrome: view/navigate directories & and view files
Mac:
  • ftp: works like on Linux
  • Chrome: view/navigate directories & and view files
  • Finder: connects, but locks in the attempt to display the top level directory w/o any activity visible at the server
Windows 10:
  • explorer: works fine, but for editing files you have to copy them to your PC and back.
  • ftp: connects, but no files operations, since passive mode is not supported
  • filezille: works as expecetd
Background operation is not implemented yet, and the file must be compiled with the image (placed in esp8266/modules), since it got too large for REPL start.
uftpserver.zip
(2.13 KiB) Downloaded 447 times

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: FTP Server

Post by chrisgp » Sat Oct 08, 2016 3:02 am

Nice, thanks for continuing to share your changes. Even if our implementations end up diverging it's nice to have something to compare to. I took a look at the current state of yours and it makes sense to me and my only feedback might be to not depend on the uos module to track the cwd and instead track it separately so that if it does end up running in the background it doesn't conflict if other scripts are depending on that.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: FTP Server

Post by Roberthh » Sat Oct 08, 2016 10:50 am

Hello @chrisgp, the point about background operation is valid. So I changed it back to your style of maintaining cwd locally. The list of tested hosts and tools is the same. Just some more observations:
- the most robust tools tested are: plain ftp, fireftp and filezilla
- the file managers besides mac finder do the job too, but with some limitations. But viewing the files & copying flles in & out works always.
- Windows file explorer does not always terminate the session when you close it.
uftpserver.zip
(2.08 KiB) Downloaded 385 times

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: FTP Server

Post by Roberthh » Sat Oct 08, 2016 8:19 pm

Hello all & @chrisgp, I made a few more changes:
  • SYST command: returns now a value which pleases Firefox
  • LIST and NLST accept as optional parameter now also filenames, with ? and * as wildcards. Now, also ftp's mput, mget and mdelete commands are supported
  • APPE command supported (may be dropped)
  • Chunk size for file transfers increased form 128 to 512 bytes, speeding up transfer.
Still no luck with OS X's Finder. The file managers on other systems works reasonable, which is especially helpful for Windows. Otherwise, the server looks stable. The most common problem is, that some clients do not (always) disconnect, like Windows Explorer or Firefox. Chris, did you make progress to move the ftp server into the background?
uftpserver.zip
(2.41 KiB) Downloaded 531 times

Post Reply