|
|
|
|
 python的ftp 实用小脚本,转贴。 - solaris7 [ 2007-02-08 11:10 | 2,522 byte(s)]
 Re: python的ftp 实用小脚本,转贴。 - yohji [ 2007-03-25 22:01 | 5,297 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
python中的ftp模块学习(pt007 原创)
1.ftpdown.py-从FTP服务器下载指定文件
#!c:python25python
#author by MSN:pt007@vip.sina.com
#create date: 2007/02/05
#description: Using ftplib module download a file from a ftp server.
from ftplib import FTP
ftp_server='127.0.0.1'
port='21'
username='test'
password='test'
ftp=FTP()
#ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.connect(ftp_server,port) #连接
ftp.login(username,password) #登录,如果匿名登录则用空串代替即可
print ftp.getwelcome() #显示ftp服务器欢迎信息
ftp.retrlines('LIST') #列出目录和文件名
path=raw_input('Enter an path:')
while path=='':
path=raw_input('Enter an path:')
ftp.cwd(path) #选择操作目录
ftp.retrlines('LIST')
bufsize = 1024 #设置缓冲块大小
filename=raw_input('Enter an filename:')
while filename=='':
filename=raw_input('Enter an filename:')
file_handler = open(filename,'wb').write#以写模式在本地打开文件
ftp.retrbinary('RETR '+filename,file_handler,bufsize) #接收服务器上文件并写入本地文件
ftp.set_debuglevel(0) #关闭调试
ftp.quit() #退出ftp服务器
2.ftppput.py-上传指定文件至FTP服务器
#!c:python25 estpython
#author by MSN:pt007@vip.sina.com
#create date: 2007/02/06
#description: Using ftplib module upload a file to a ftp server.
from ftplib import FTP
ftp_server='127.0.0.1'
port='21'
username='administrator'
password='test'
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect(ftp_server,port)
ftp.login(username,password)
print ftp.getwelcome()
ftp.retrlines('LIST') #列出目录和文件名
path=raw_input('Enter an path:')
while path=='':
path=raw_input('Enter an path:')
ftp.cwd(path) #选择操作目录
bufsize = 1024
filename=raw_input('Enter an filename:')
while filename=='':
filename=raw_input('Enter an filename:')
file_handler = open(filename,'rb') #返回文件描述字对象
ftp.storbinary('STOR %s'%filename,file_handler,bufsize) #上传文件
ftp.set_debuglevel(0)
file_handler.close() #关闭文件
ftp.quit()
|
|
|
----
像你一样,骄傲,不驯,而且敏捷
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
我自己写的一个:
1 #!/usr/bin/env python
2 import getopt, sys
3 import ftplib
4 class myftp(object):
5 def __init__(self):
6 return
7 def ftpConnect(self, host):
8 msg = ''
9 global verbose
10 self.f = ftplib.FTP(host);
11 self.f.login(username, password)
12 msg = self.f.getwelcome()
13 if verbose:
14 print msg
15 return
16 def ftpSendCmd(self, cmds):
17 msg = ''
18 global verbose
19 if cmds[0] == 'LIST' or cmds[0] == 'list':
20 msg = self.f.retrlines(msg.join(cmds))
21 elif cmds[0] == 'CD' or cmds[0] == 'cd':
22 msg = self.f.cwd(cmds[1])
23 else:
24 msg = self.f.sendcmd(msg.join(cmds))
25 if verbose:
26 print msg
27 return
28 def ftpClose(self):
29 self.f.close()
30 def usage():
31 print sys.argv[0]+":"
32 print '''
33 [-h|--help] [-V|--version]
34 [|-v|--verbose] [-u USER|--user=USER] [-p PASS| --pass=PASS] [host]'''
35 return
36 def showversion():
37 print "0.1"
38 return
39 def main():
40 global verbose
41 if len(sys.argv) < 2:
42 print "Bad usage!"
43 print "
"
44 usage()
45 return
46 else:
47 opts, args = getopt.getopt(sys.argv[1:],
48 "hvVu:p:", ["help", "verbose", "version", "user", "pass"])
49 if len(args) > 1:
50 print "Can't specify more hosts."
51 return
52 else:
53 for o, a in opts:
54 if o in ("-h", "--help"):
55 usage()
56 sys.exit()
57 if o in ("-V","--version"):
58 showversion()
59 sys.exit()
60 if o in ("-v", "--verbose"):
61 verbose = 1
62 if o in ("-u", "--user"):
63 username = a
64 if o in ("-p", "--pass"):
65 password = a
66 if len(args)==0:
67 print "You didn't specify a host!"
68 sys.exit()
69 host = args[0]
70 m = myftp()
71 print 'Connecting...'
72 m.ftpConnect(host)
73 print 'OK.'
74 while 1:
75 cmd = raw_input('>')
76 cmds = cmd.split()
77 if not cmds[0] in clientCmds:
78 print "Wrong command: "+cmd+"."
79 continue
80 elif cmds[0] == 'QUIT' or cmds[0]=='quit':
81 break
82 else:
83 m.ftpSendCmd(cmds)
84 m.ftpClose()
85 return
86 if __name__=='__main__':
87 verbose = 0
88 username = "anonymous"
89 password = ""
90 ftpCmds = ['LIST','list','CD','cd','PWD','pwd']
91 clientCmds = ftpCmds+['QUIT','quit']
92 main()
|
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
|