f
tplib模块定义了FTP类和一些方法,用以进行客户端的ftp编程。可以用python编写一个自已的ftp客户端程序,用于下载文件或镜像站点。如果想了解ftp协议的详细内容,请参考RFC959。
该模块是python的通用模块,所以默认应该已安装。ftplib模块使用很简单,暂时只有一个FTP类和十几个函数。
该模块是python的通用模块,所以默认应该已安装。ftplib模块使用很简单,暂时只有一个FTP类和十几个函数。
下面用一个交互方式演示一下ftplib的主要功能。
>>> from ftplib import FTP >>> ftp = FTP('ftp.cwi.nl') # connect to host, default port >>> ftp.login() # user anonymous, passwd anonymous@ >>> ftp.retrlines('LIST') # list directory contents total 24418 drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 . dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 .. -rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX . . . >>> ftp.retrbinary('RETR README', open('README', 'wb').write) '226 Transfer complete.' >>> ftp.quit()
下面一个下载文件的示例
#!/usr/bin/env python
#author:Jims of http://www.ringkee.com/
#create date: 2005/02/05
#description: Using ftplib module download a file from a ftp server.
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.connect('ftp_server','port') #连接
ftp.login('username','password') #登录,如果匿名登录则用空串代替即可
print ftp.getwelcome() #显示ftp服务器欢迎信息
ftp.cwd('xxx/xxx/') #选择操作目录
bufsize = 1024 #设置缓冲块大小
filename='dog.jpg'
file_handler = open(filename,'wb').write #以写模式在本地打开文件
ftp.retrbinary('RETR dog.jpg',file_handler,bufsize) #接收服务器上文件并写入本地文件
ftp.set_debuglevel(0) #关闭调试
ftp.quit() #退出ftp服务器
下面一个上传文件的示例,要成功运行该脚本,需在ftp服务器上有上传文件的权限。
#!/usr/bin/env python
#author:Jims of http://www.ringkee.com/
#create date: 2005/02/05
#description: Using ftplib module upload a file to a ftp server.
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect('ftp_server','port')
ftp.login('username','password')
print ftp.getwelcome()
ftp.cwd('xxx/xxx/')
bufsize = 1024
filename='dog.jpg'
file_handler = open(filename,'rb')
ftp.storbinary('STOR dog.jpg',file_handler,bufsize) #上传文件
ftp.set_debuglevel(0)
file_handler.close() #关闭文件
ftp.quit()
#!/usr/bin/env python
#author:Jims of http://www.ringkee.com/
#create date: 2005/02/05
#description: Using ftplib module download a file from a ftp server.
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.connect('ftp_server','port') #连接
ftp.login('username','password') #登录,如果匿名登录则用空串代替即可
print ftp.getwelcome() #显示ftp服务器欢迎信息
ftp.cwd('xxx/xxx/') #选择操作目录
bufsize = 1024 #设置缓冲块大小
filename='dog.jpg'
file_handler = open(filename,'wb').write #以写模式在本地打开文件
ftp.retrbinary('RETR dog.jpg',file_handler,bufsize) #接收服务器上文件并写入本地文件
ftp.set_debuglevel(0) #关闭调试
ftp.quit() #退出ftp服务器
下面一个上传文件的示例,要成功运行该脚本,需在ftp服务器上有上传文件的权限。
#!/usr/bin/env python
#author:Jims of http://www.ringkee.com/
#create date: 2005/02/05
#description: Using ftplib module upload a file to a ftp server.
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect('ftp_server','port')
ftp.login('username','password')
print ftp.getwelcome()
ftp.cwd('xxx/xxx/')
bufsize = 1024
filename='dog.jpg'
file_handler = open(filename,'rb')
ftp.storbinary('STOR dog.jpg',file_handler,bufsize) #上传文件
ftp.set_debuglevel(0)
file_handler.close() #关闭文件
ftp.quit()
本文转自博客园刘凯毅的博客,原文链接:
python ftp (转)
,如需转载请自行联系原博主。