Voder-Vocoder

The Log of Hal Canary

Navigation: Home | THE LOG | Log Archives | Resume | Contact Info | Public Key | SSL | Math Applets | Feedback Form | Site Map | WP Backend | RSS2 | Atom

« Downsample | Email2 »

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()

Hal Canary | Computers & Code | 2008-01-13 00:05:28 EST
Permanent Link | No Comments

Comments are closed.

Copyright 1997-2007 by Hal Canary.
mailto: h3 at halcanary dot org
xmpp:halcanary@jabber.org
aim:halwcanary
http://halcanary.org