FTP 上传下载
评论(0) 浏览量(5613)

上传:

# -*- coding: cp936 -*-
'''
Created on 2011-8-4

@author: flyfox

@function:下载path路径下的file文件,下载到当前文件
'''
from ftplib import FTP
import sys
def ftpdownload(path,file):
    user ='root'
    password = 'password'
    ftp = FTP()
    ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
    ftp.connect('10.16.9.134') #连接ftp服务器
    ftp.login(user,password) #输入用户名和密码
    print ftp.getwelcome() #显示ftp服务器的欢迎信息
    ftp.cwd(path) #选择操作目录
    bufsize = 1024 #设置缓冲区大小
    file_handler = open(file,'wb').write #以写模式在本地打开文件
    strBuffer = 'RETR ' + file
    ftp.retrbinary(strBuffer,file_handler,bufsize) #接收服务器上文件并写入本地文件
    ftp.set_debuglevel(0) #关闭调试
    ftp.quit() #退出ftp服务器
if __name__ == '__main__':
    path1 = '/ultrapower/test'
    file1 = 'test.txt'
    if len(sys.argv) == 3:
        try:
            ftpdownload(sys.argv[1],sys.argv[2])    #命令行输入文件在ftp上的路径和文件名,       
        except IOError:
            print "please input the correct path and filename"
    else:
        ftpdownload(path1,file1)

下载:

# -*- coding: cp936 -*-

'''
Created on 2011-8-4

@author: flyfox

@function:上传当前路径的file文件到服务器的path路径下
'''
from ftplib import FTP
import sys,os
def ftpdownload(path,file):
    user ='root'
    password = 'password'
    ftp = FTP()
    ftp.set_debuglevel(2)
    ftp.connect('10.16.9.134')
    ftp.login(user,password)
    print ftp.getwelcome()
    ftp.cwd(path)
    bufsize = 1024
    file_handler = open(file,'rb') #读方式打开上传文件
    strBuffer = 'STOR ' + file
    ftp.storbinary(strBuffer,file_handler,bufsize) #上传文件
    ftp.set_debuglevel(0)
    ftp.quit()
if __name__ == '__main__':
    path1 = '/ultrapower/test'
    file1 = 'test.txt'
    if len(sys.argv) == 3:
        try:
            ftpdownload(sys.argv[1],sys.argv[2])           
        except IOError:
            print "please input the correct path and filename"
    else:
        ftpdownload(path1,file1)

没有登录不能评论