#!/usr/bin/env python """ Author: Tom Luo 11/08/2010 """ _author__ = "Tom Luo" __version__ = "$Revision 0.1 $" __date__ ="$Date: 2011/11/08 19:57 MST$" import sys, os from ftplib import FTP def getFTP(host,user,passwd): try: handle = FTP(host) except: print "Host could not be resolved." sys.exit() try: handle.login(user,passwd) except Exception: print "Invalid login." sys.exit() else: print "Connected!" # print handle.getwelcome() return handle def upload(handle,filename,remotedir): try: handle.cwd(remotedir) print "uploading %s to %s" % (filename, handle.pwd()) except Exception: print "remote dir %s doesn't exist" %remotedir handle.quit() sys.exit() f = open(filename,"rb") (base,ext) = os.path.splitext(filename) exts = [".bmp", ".jpg", ".gif",".png"] if(ext in exts): try: handle.storbinary("STOR " + filename,f) except Exception: print "Failed to upload." else: print "Done." f.close() return else: try: handle.storlines("STOR " + filename,f) except Exception: print "Failed to upload." else: print "Done" f.close() return if __name__ == "__main__": host = 'your_ftp_server' user = 'your_user' passwd = 'passwd' remotedir = 'oracleabc.com/b/pics' if len(sys.argv) <> 2: print "Usage: myftp.py filename" else: # filename = raw_input("File to upload: ") # passwd = raw_input("password: ") ftp = getFTP(host,user,passwd) upload(ftp,sys.argv[1],remotedir)