fcp (FTP copy)

A ftp client that uses rcp/scp syntax

By Hal Canary, 2008-01-13 00:05:28 (link)
#computers-code

My XO-1 has gotten me interested in hacking in python again, since that is the XO's primary programming environment.

I quickly hacked out an ftp script in python since the default install lacked an ftp client. "My next project will be to finally write version 1.0 of my fcp (FTP copy) program --- a ftp client that uses rcp/scp syntax.

Here's the first script:

#!/usr/bin/env python2.5
import ftplib
import os
import getpass
host="hostname"
user="hal"
pas=getpass.getpass(user+"@"+host+"'s password: ")
# local file with path
localfile="ftpcp"
# directory  on sever
dir="/www/pub"
# filrnaame on server
filename="ftpcp.txt"
# login to FTP
f=ftplib.FTP(host)
f.login(user, pas)
f.set_pasv(True)
f.cwd(dir)
f.storbinary("STOR " + filename, open(localfile, 'rb'))
f.quit()

Next I've got to write some very savy code that can parse complex command-line options.

Then I've got to learn how to do everything else in python.


Update: a function!

import ftplib
import os
import getpass
def ftpputfiles(host, direct, *files, **kwds):
    'user defaults to $USERNAME if blank'
    # does little error checking.  this is bad!
    user, password = ('', '')
    if kwds.has_key('user'):
        user = kwds['user']
    if user == '':
        user = os.environ['USERNAME']

    if kwds.has_key('password'):
        password = kwds['password']
    if password == '':
        querystring = '%s@%s\'s password: ' % (user,host)
        password = getpass.getpass(querystring)

    f = ftplib.FTP(host)
    f.login(user, password)
    f.set_pasv(True)
    f.cwd(direct)
    for localfile in files :
        filename = os.path.basename(localfile)
        fl = open(localfile, 'rb')
        f.storbinary('STOR %s' % filename, fl)
        fl.close()
    f.quit()

(back)