python ftp测试

2023-01-31 01:01:24 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'

相关文章