| Blog信息 |
|
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7693909 建立时间:2006年5月29日 |

| |
|
[Python]一个FTP小工具 软件技术
lhwork 发表于 2007/1/10 9:44:37 |
| 百度空间上的背景音乐时不时会更新下,每次都是通过FlashFXP上传的。又是登录,又是选择目录的,着实麻烦。于是写了这个小工具。 开发环境:Eclipse3.2+Pydev+Python2.4 技术上,没有太多的新意,几乎就是ftplib的基础应用。在开发方式上,通过写“死”的测试Demo,抽取出“活”的部分。整个迭代过程,比较自然。思路会随着写的过程,逐渐清晰。一些在设计之初没考虑到的细节部分,也会中途逐渐显现。 主要功能有三个。list,显示ftp站点某目录的内容;download,下载文件到指定目录下;upload,上传文件到指定目录下。 还是看代码,比较简单。一定程度上,考虑到了程序的可扩展性。conf/config.xml:
1 500)this.width=500'> <? xml version="1.0" encoding="UTF-8" ?> 2 500)this.width=500'> < configuration > 3 500)this.width=500'> < host > 10.0.0.1 </ host > 4 500)this.width=500'> < user > username </ user > 5 500)this.width=500'> < password > password </ password > 6 500)this.width=500'> </ configuration >
modules/parseConf.py:
1 500)this.width=500'> # Programmer: qixiang 2 500)this.width=500'># E-mail: mail2qixiang AT gmail DOT com 3 500)this.width=500'># 4 500)this.width=500'># Copyleft 2006 qixiang 5 500)this.width=500'># 6 500)this.width=500'># modules/parseConf.py 2006-11-19 03:56:15 qixiang 7 500)this.width=500'> 8 500)this.width=500'> def getNodeValue(filename, nodename): 9 500)this.width=500'> from xml.dom import minidom10 500)this.width=500'> xmldoc = minidom.parse(filename)11 500)this.width=500'> nodeValue = xmldoc.getElementsByTagName(nodename)[0].firstChild.data12 500)this.width=500'> return nodeValue
modules/handleFTP.py:
1 500)this.width=500'> # Programmer: qixiang 2 500)this.width=500'># E-mail: mail2qixiang AT gmail DOT com 3 500)this.width=500'># 4 500)this.width=500'># Copyleft 2006 qixiang 5 500)this.width=500'># 6 500)this.width=500'># modules/handleFTP.py 2006-11-19 04:04:25 qixiang 7 500)this.width=500'> 8 500)this.width=500'> from ftplib import FTP 9 500)this.width=500'> def login(host, user, password):10 500)this.width=500'> try :11 500)this.width=500'> ftp = FTP(host)12 500)this.width=500'> # print 'connect successfully' 13 500)this.width=500'> try :14 500)this.width=500'> ftp.login(user = user, passwd = password)15 500)this.width=500'> # print 'login successfully' 16 500)this.width=500'> return ftp17 500)this.width=500'> except :18 500)this.width=500'> print ' incorrect user or password ' 19 500)this.width=500'> except :20 500)this.width=500'> print ' unconnected host ' 21 500)this.width=500'> 22 500)this.width=500'> def list(ftp, ftpDirectory):23 500)this.width=500'> try :24 500)this.width=500'> ftp.cwd(ftpDirectory)25 500)this.width=500'> ftp.retrlines( ' LIST ' )26 500)this.width=500'> ftp.close()27 500)this.width=500'> except :28 500)this.width=500'> print ' incorrect directory ' 29 500)this.width=500'> 30 500)this.width=500'> def upload(ftp, remoteFile, localFile):31 500)this.width=500'> try :32 500)this.width=500'> ftp.storlines( ' STOR ' + remoteFile, open(localFile))33 500)this.width=500'> print ' upload successfully ' 34 500)this.width=500'> print 35 500)this.width=500'> ftp.retrlines( ' LIST ' )36 500)this.width=500'> ftp.close()37 500)this.width=500'> except :38 500)this.width=500'> print ' incorrect remoteFile or localFile ' 39 500)this.width=500'> 40 500)this.width=500'> def download(ftp, remoteFile, localFile):41 500)this.width=500'> try :42 500)this.width=500'> ftp.retrlines( ' RETR ' + remoteFile, open(localFile, " w " ).write) 43 500)this.width=500'> print ' download successfully ' 44 500)this.width=500'> print 45 500)this.width=500'> ftp.retrlines( ' LIST ' )46 500)this.width=500'> ftp.close()47 500)this.width=500'> except :48 500)this.width=500'> print ' incorrect remoteFile or localFile '
ftp.py :
1 500)this.width=500'> # Programmer: qixiang 2 500)this.width=500'># E-mail: mail2qixiang AT gmail DOT com 3 500)this.width=500'># 4 500)this.width=500'># Copyleft 2006 qixiang 5 500)this.width=500'># 6 500)this.width=500'># ftp.py 2006-11-19 04:04:54 qixiang 7 500)this.width=500'> 8 500)this.width=500'> import sys 9 500)this.width=500'> from modules.parseConf import getNodeValue10 500)this.width=500'> from modules.handleFTP import login, list, upload, download11 500)this.width=500'> 12 500)this.width=500'>configFile = ' conf/config.xml ' 13 500)this.width=500'>flag = sys.argv[ 1 ]14 500)this.width=500'>ftp = login(getNodeValue(configFile, ' host ' ), getNodeValue(configFile, ' user ' ), getNodeValue(configFile, ' password ' ))15 500)this.width=500'> 16 500)this.width=500'> if flag == ' list ' :17 500)this.width=500'> list(ftp, sys.argv[ 2 ])18 500)this.width=500'> if flag == ' upload ' :19 500)this.width=500'> upload(ftp, sys.argv[ 2 ], sys.argv[ 3 ])20 500)this.width=500'> if flag == ' download ' :21 500)this.width=500'> download(ftp, sys.argv[ 2 ], sys.argv[ 3 ])
命令行下运行: 1. list python ftp.py list ftp目录名 eg. python ftp.py list music 2. upload python ftp.py upload 上传所要保存的ftp目录文件名 待上传的本地文件 eg. python ftp.py upload music\remote.xml c:\local.xml 3. download python ftp.py download 待下载的远程文件 下载所要保存的本地目录文件名 eg. python ftp.py download music\remote.xml c:\local.xml 我个人是将实现upload功能的命令,保存成一个批处理文件。需要的时候,运行下就OK了。upload.bat:
500)this.width=500'> d:500)this.width=500'>cd java/eclipse/workspace/ftp500)this.width=500'>python ftp.py upload music\mp3player.xml f:\website\music\mp3player.xml
功能比较单一,还有许多欠考虑的地方。例如对上传文件类型的判断,对上传、下载文件所在目录存在与否的判断,等等。 小的东西,如果深究,也蛮有意思。 ps: 程序目录及copyleft部分借鉴了limodou的Ulipad,在此表感谢。 |
|
|