ftplibでgetとかputとか

python 2.7に標準添付される、ftplibはここにマニュアルがあるんだが、ちょっと、悩んだのでメモを残す。
細かい制御ができるんだが、もっと粒度を大きくして使い慣れた、putとかgetとの対応が欲しかったのでコメントを付けてみた。

# python2.7 の例
from ftplib import FTP

def ftpput(host, username, password, putfile):
    try:
        _ftp = FTP(host, username, password)  # open(username, password)
        _ftp.cwd("/var/tmp/")            # cd /var/tmp ディレクトリの移動
        _file = open(putfile, 'rb')      # bin バイナリモード
        command = "STOR " + putfile
        _ftp.storbinary(command, _file)  # put putfile
        _file.close()
        _ftp.quit()                      # bye
    except:
        print "ftpput_failed :" + putfile

def ftpget(host, username, password, getfile):
    try:
        _ftp = FTP(host, username, password)  # open(username, password)
        _ftp.cwd("/var/tmp/")            # cd /var/tmp ディレクトリの移動
        _file = open(getfile, "wb")      # bin バイナリモード
        command = "RETR " + getfile
        _ftp.retrbinary(command, _file.write)
                                         # get getfile
        _file.close()
        _ftp.quit()                      # bye
    except:
        print "ftpget_failed :" + getfile 

if __name__ == '__main__':
    # こう使う。
    ftpput("192.168.0.10", "testuser", "testpass", "./aaaa.html")
    ftpget("192.168.0.10", "testuser", "testpass", "bbbb.zip")