Python编程-FTP

一个简单的ftp程序,实现从服务器下载文件和上传文件


#!/usr/bin/env python

import sys
import os
from ftplib import FTP
import time

def ftp_down():
    hftp = FTP('ip')
    hftp.login('user','passwd')
    hftp.cwd('serverpath')

    os.chdir('localpath')

    flst = hftp.nlst()
    for filename in flst:
        print filename
        hftp.retrbinary('RETR ./%s' % filename,file(filename,'wb').write)
    hftp.close()

def ftp_up():
    hftp = FTP('ip')
    hftp.login('user','passwd')
    hftp.cwd('serverpath')

    os.chdir('localpath')
    hftp.storbinary('STOR %s' % os.path.basename('1.txt'), file('1.txt','rb'))

    hftp.close()

def main():
    ftp_down()
    ftp_up()
    pass

if __name__ == '__main__':
    main()



你可能感兴趣的:(Python编程-FTP)