python ftp测试
刚学Python,做的ftp测试
1,简单ftp
vim test.py
#!/usr/bin/env python
import time
from ftplib import FTP
local_dir_update="*****"
local_dir_download="*******"
remote_host="*.*.*.*"
port="21"
remote_user="name"
remote_passwd="password"
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect(remote_host,port)
ftp.login(remote_user,remote_passwd)
print ftp.getwelcome()
#ftp.cwd()
bufsize = 1024
filename='ha.txt'
file_handler = open(filename,'rb')
ftp.storbinary('STOR ha.txt',file_handler,bufsize)
ftp.set_debuglevel(0)
file_handler.close()
ftp.quit()
2.稍复杂点
vim myftp.py
#!/usr/bin/env python
import time,socket
from ftplib import FTP
class MyFtp:
def __init__(self,hostaddr,username,password,remotedir='./',port=21):
self.hostaddr=hostaddr
self.username = username
self.password = password
self.remotedir = remotedir
self.port = port
self.ftp = FTP()
self.file_list = []
def __del__(self):
self.ftp.quit()
def login(self):
ftp = self.ftp
try:
timeout = 60
socket.setdefaulttimeout(timeout)
ftp.set_pasv(True)
print 'connecting %s...' %(self.hostaddr)
ftp.connect(self.hostaddr, self.port)
ftp.login(self.username, self.password)
print 'login is ok'
except Exception,e:
print str(e)
try:
ftp.cwd(self.remotedir)
except(Exception):
print 'change dir is failed..'
def put_files(self,filename):
ftp = self.ftp
self.filename=filename
command='STOR '+self.filename
file_handler = open(self.filename,'rb')
bufsize=1024
ftp.storbinary(command,file_handler,bufsize)
print 'upload is ok'
file_handler.close()
def download_files(self,filename):
ftp = self.ftp
self.filename = filename
command='RETR '+self.filename
bufsize=1024
ftp.retrbinary(command,open(self.filename,'wb').write,bufsize)
if __name__ == '__main__':
remote_host="*******"
remote_user="name"
remote_passwd="*******"
remotedir='./'
port='21'
filename='ha.txt'
f = MyFtp(remote_host,remote_user,remote_passwd,remotedir,port)
f.login()
# f.put_files(filename)
# f.download_files(filename)
a=['ha.txt','ha1.txt','ha2.txt']
for a1 in a:
f.download_files(a1)
print 'download is ok'
3.与上面配合写的一个,在一个特定目录下的文件执行脚本到服务器上取.zip文件。(工作中用到,才写)
vim download.py
#!/usr/bin/env python
import os,time
from myftp import MyFtp
file_list=['xxxxxx','xxxxxx,'xxxxxx','xxxxxx','xxxxxx']
local_dir_update="/xxxxxx"
local_dir_download="/xxxxxx"
a=time.localtime(time.time()+24*60*60)
dt=time.strftime('%Y%m%d',a)
if os.path.exists(local_dir_download+os.sep+dt):
print "ok"
else:
os.mkdir(local_dir_download+os.sep+dt)
if __name__ == '__main__':
remote_host="xxxxxx"
remote_user="cxxxxxx"
remote_passwd="xxxxxx"
remotedir=dt
port='21'
filename='ha.txt'
f = MyFtp(remote_host,remote_user,remote_passwd,remotedir,port)
f.login()
# f.put_files(filename)
# f.download_files(filename)
a=os.listdir(local_dir_update)
os.chdir(local_dir_download+os.sep+dt)
for a1 in a:
if a1 in file_list:
f.download_files(a1+".zip")
print 'download is ok'
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341