Programmable FTP Client

FTP may not be as secure as SFTP but it is very fast. SFTP is best for personal usage. If you are sharing non-critical files among group of users, FTP seems to be better choice. Note that there is no shell access for those users. For example, a set of users are sharing a server space placed at a high-speed data center. Everyone placed requests to download new files everyday. In other words, all users download files to temporarily store in the server and then get them back home through FTP.

The FTP protocol has many issues left to the implementation both server and client. In addition, most servers and clients do not strictly follow the standard. As a result, some clients might not work well with some servers. I don’t want to blame anyone. It’s just a way to walk through.

Since there are so many new files appeared on the server, downloading one by one is a painful routine process, especially for sub-folders. My friends reports good feedback for SmartFTP. Unfortunately, my computer is Linux. Both NcFTP and lftp do not work perfectly for me. They often stuck during download and I have to restart them again. lftp has lots of problem regarding white-space in filename and directory name.

My solution is to develop my own FTP client to fit my needs.

  • Resumable
  • Recursive
  • Scan and filter
  • Automatic restart if transfer speed is lower than threshold
  • Platform independent
  • Support i18n
  • Compatible with all servers

Developing an FTP client from scratch is not fun. Fortunately, I know a bit about ftplib in Python and I found ftputil for Python. Most of essential functionalities are already implemented in ftputil. I just need to write a wrapper to measure download rate and force it to restart when necessary.

import ftputil

# download some files from the login directory
host = ftputil.FTPHost(’ftp.domain.com’, ’user’, ’password’)
names = host.listdir(host.curdir)
for name in names:
    if host.path.isfile(name):
        host.download(name, name, ’b’)  # remote, local, binary mode

# make a new directory and copy a remote file into it
host.mkdir(’newdir’)
source = host.file(’index.html’, ’r’)  # file-like object
target = host.file(’newdir/index.html’, ’w’)  # file-like object
host.copyfileobj(source, target)  # similar to shutil.copyfileobj
source.close()
target.close()

Technorati Tags: , , , , , , , , ,

Post new comment