python ftp 文件上传和文件下载

import ftplib
import os
import socket

HOST='192.168.30.109'
FILE='test'

def main():
    try:
        f=ftplib.FTP(HOST)
    except (socket.error,socket.gaierror),e:
        print 'ERROR:cannot reach "%s"'% HOST
        return
    print '***connected to host "%s"' % HOST
    
    try:
        f.login(user='test',passwd='123')
    except ftplib.error_perm:
        print 'ERROR:cannot login anonymously'
        f.quit()
        return
    print '***Logged in as "test""' 
    
    try:
        f.retrbinary('RETR %s' % FILE,open(FILE,'wb').write)
    except ftplib.error_perm:
        print 'ERROR:cannot read file "%s"' % FILE
        os.unlink(FILE)
    else:
        print '***Downloaded "%s" to CWD' % FILE
        return
    try:
        f.storbinary('STOR %s' % FILE,open(FILE,'rb'))
    except ftplib.error_perm:
        print 'ERROR:cannot up file "%s"' % FILE
        os.unlink(FILE)
    else:
        print '***upload "%s" to ftp' % FILE
        f.quit()
        return
  
if __name__=='__main__':
    main()


你可能感兴趣的:(python,File,upload,user,import,login)