开发者社区> 技术小阿哥> 正文

python ftp

简介:
+关注继续查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
ftp
 
'''第一个例子'''
def get_C(self,target_dir=None):
        = []
        print "PWD:"self.ftp.pwd()
        if target_dir is not None:
            self.ftp.cwd(target_dir)# change working directory to target_dir
        server_file_list = []
        fuck_callback = lambda x: (server_file_list.append(x))
        self.ftp.retrlines('LIST', fuck_callback)
        # print server_file_list
        server_file_items = self.filter_dir_list(server_file_list)
        for item in server_file_items:
            if item.is_dir:
                print 'name = ', item.name
                sub_C = self.get_C(item.name)
                # sub_C = ['/'+item.name+'/'+cc.name for cc in sub_C]
                for cc in sub_C:
                    cc.name = '/' + item.name + cc.name
                    print 'name --- ',cc.name
                C.extend(sub_C)
            else:
                item.name = '/' + item.name
                C.append(item)
        self.ftp.cwd('..')
        return C
 
def runtest(self,next_dir):
        = ftp.get_C(next_dir)
        next_dir2=next_dir[2:]
        = [cc.pack for cc in C]
        for in C:
            print i
            next_dir1=i
            pos=next_dir1.rindex('/')
            next_dir3= next_dir1[0:pos]
            all_path=next_dir2 + next_dir3
            print all_path
            next_dir_local = all_path.decode('utf8').encode('gbk')
            try:
                print next_dir_local
                #os.makedirs(next_dir_local)
            except OSError:
                pass
            #os.chdir(next_dir_local)
            localfile=next_dir1[pos+1:]
            print localfile
            allall_path=all_path + "/" + localfile
            self.ftp.cwd('/')
            print self.ftp.pwd()
            #file_handler = open(localfile, 'wb')
            #self.ftp.retrbinary('RETR %s' % (allall_path), file_handler.write)
            #file_handler.close()
 
'''第一个例子获取成/home/user/test.txt这样的列表'''
 
 
第二个例子
def download_files(self, localdir='./', remotedir='./'):
        try:
            self.ftp.cwd(remotedir)
        except:
            debug_print('目录%s不存在,继续...' % remotedir)
            return
        if not os.path.isdir(localdir):
            pass
            #os.makedirs(localdir)
        debug_print('切换至目录 %s' % self.ftp.pwd())
        self.file_list = []
        self.ftp.dir(self.get_file_list)
        remotenames = self.file_list
        print(remotenames)
        # return
        for item in remotenames:
            filetype = item[0]
            filename = item[1]
            print "filename:",filename
            local = os.path.join(localdir, filename)
            if filetype == 'd':
                self.download_files(local, filename)
            elif filetype == '-':
                self.download_file(local, filename)
        self.ftp.cwd('..')
        debug_print('返回上层目录 %s' % self.ftp.pwd())
 
f.download_files(rootdir_local, rootdir_remote)
 
'''第二个例子'''

区别很大

ftp:

ftp.retrlines('LIST', fuck_callback)

完全是循环,目录的进行循环操作,而文件下载。最底层目录的文件下载完,回归上级目录。继续循环。


self.ftp.pwd()

self.ftp.dir(self.get_file_list)

get_file_list(self, line)

self.ftp.cwd('..')

self.ftp.cwd(remotedir)

self.download_file(local, filename)

建立好本地目录,然后cd到远程目录,下载


代码格式乱了,详细例子

ftp 第一个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# !/usr/bin/env python
# -*-coding:utf-8-*-
from ftplib import FTP
from time import sleep
import os, datetime,logging,time
import string,re
d1 = datetime.datetime.now()
'''months=['Jan','Feb','March','Apr','May','Jun','Jul','Aug','Sep']
patternm = r'2017.*|201611.*|201612.*|201610.*'
patternxml = r'.*2016'
patternx = r'xx.*'''''
HOST = "192.168.1.100"
USER = "ftpuser3"
PASSWORD = "test1passwd"
 
class Myfile(object):
    def __init__(self, name, size, mtime):
        self.name = name  # 文件名字
 
        self.mtime = mtime  # 文件创建时间
        self.is_dir = False   # 是否为文件夹,默认为不是文件夹
 
        #self.size = float(size) / (1024 * 1024)  # 文件大小
        size = float(size)
        if size > 1024*1024:
            self.size = str('%.2f'%(size / (1024*1024))) + 'MB'
        elif size > 1024:
            self.size = str('%.2f'%(size / 1024)) + 'KB'
        else:
            self.size = str(size) + 'Bytes'
    @property
    def is_file(self):
        return not self.is_dir
 
    @property
    def dir_property(self):
        if self.is_dir==True:
            return 'dir'
        return 'file'
 
    def show(self):
        print '[%s], [%s], [%s], [%s]' % (self.name, self.size, self.mtime, self.dir_property)
 
    @property
    def pack(self):
        """
        将myfile对象封装为一个字符串
        :return:
        """
        #return '[%s][%s][%s]'%(self.name, self.size, self.mtime)
        #return '[%s][%s]'%(self.name, self.size)
        return '%s' %(self.name)
 
class CLASS_FTP:
    def __init__(self, HOST, USER, PASSWORD, PORT='21'):
        self.HOST = HOST
        self.USER = USER
        self.PASSWORD = PASSWORD
        self.PORT = PORT
        self.ftp = FTP()
        self.flag = 0  # 0:no connected, 1: connting
 
    def Connect(self):
        try:
            if self.flag == 1:
                logging.info("ftp Has been connected")
            else:
                self.ftp.connect(self.HOST, self.PORT)
                self.ftp.login(self.USER, self.PASSWORD)
                # self.ftp.set_pasv(False)
                self.ftp.set_debuglevel(0)
                self.flag = 1
        except Exception:
            logging.info("FTP login failed")
 
    def str_codec_std(self,mystr):
        return mystr.decode('utf8').encode('gbk')
 
    def dirmakedirs(self,next_dir_local,local_dir):
        # next_dir_local2= next_dir_local.split('/')[1:]
        next_dir_local2 = next_dir_local[1:].replace('/''\\')
        # next_dir_localw = next_dir_local2.decode('utf8').encode('gbk')  # windows用这个
        s_file = os.path.join(local_dir, next_dir_local2)
        print "s_file", s_file
        if not os.path.exists(s_file):
            try:
                os.makedirs(s_file)
            except OSError:
                pass
        os.chdir(s_file)
 
    def filter_dir_list(self,mystr_list):
        res = []
        for mystr in mystr_list:
            #mystr = self.str_codec_std(mystr)
            # print "mystr is :%s" % mystr
            file_info = string.split(mystr, maxsplit=8)
            name = file_info[8]
            print 'name = ', name
            if name == '.' or name == '..':
                continue
 
            size = file_info[4]
            mtime = '%s-%s-%s' % (file_info[5], file_info[6], file_info[7])
 
            myfile = Myfile(name=name, size=size, mtime=mtime)
 
            dir_info = file_info[0]
            if dir_info[0== 'd':
                myfile.is_dir = True
            res.append(myfile)
        return res
 
 
    def get_C(self,target_dir=None,local_dir=None):
        = []
        if target_dir is not None:
            self.ftp.cwd(target_dir)# change working directory to target_dir
        server_file_list = []
        fuck_callback = lambda x: (server_file_list.append(x))
        self.ftp.retrlines('LIST', fuck_callback)
        next_dir_local = self.ftp.pwd()
        self.dirmakedirs(next_dir_local, local_dir)
 
        server_file_items = self.filter_dir_list(server_file_list)
        for item in server_file_items:
            if item.is_dir:
                sub_C = self.get_C(item.name,local_dir)
                for cc in sub_C:
                    cc.name = '/' + item.name + cc.name
                C.extend(sub_C)
            else:
                item.name = '/' + item.name
                C.append(item)
        self.ftp.cwd('..')
        return C
    def runtest(self,local_dir,next_dir):
        os.chdir(local_dir)
        = ftp.get_C(next_dir,local_dir)
        next_dir2=next_dir[2:]
        = [cc.pack for cc in C]
        print "C:",C
        for in C:
            next_dir1=i
            pos=next_dir1.rindex('/')
            next_dir3= next_dir1[0:pos]
            all_path=next_dir2 + next_dir3
 
            self.dirmakedirs(all_path, local_dir)
            next_dir_localz = all_path[1:].replace('/''\\')
            '''# next_dir_local = next_dir_localz
            # next_dir_local = next_dir_localz.decode('utf8').encode('gbk') #windows用这个'''
            # s_file = os.path.join(local_dir, next_dir_localz)
            # try:
            #     os.makedirs(s_file)
            # except OSError:
            #     pass
            # os.chdir(s_file)
 
            localfile=next_dir1[pos+1:]
            print localfile
            allall_path=all_path + "/" + localfile
            file_handler = open(localfile, 'wb')
            self.ftp.retrbinary('RETR %s' % (allall_path), file_handler.write)
            file_handler.close()
 
if __name__ == '__main__':
    ftp = CLASS_FTP(HOST, USER, PASSWORD)
    ftp.Connect()
    ftp.runtest('D:\\ftp','./')
    d2 = datetime.datetime.now()
    print d2 - d1
 
'''参数乱七八糟'''


ftp 第二个例子 别人2010写好的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# !/usr/bin/env python
# coding:utf-8
from ftplib import FTP
import os, sys, string, datetime, time
import socket
 
 
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 = []
        # self.ftp.set_debuglevel(2)
 
    def __del__(self):
        self.ftp.close()
        # self.ftp.set_debuglevel(0)
 
    def login(self):
        ftp = self.ftp
        try:
            timeout = 60
            socket.setdefaulttimeout(timeout)
            ftp.set_pasv(True)
            print '开始连接到 %s' % (self.hostaddr)
            ftp.connect(self.hostaddr, self.port)
            print '成功连接到 %s' % (self.hostaddr)
            print '开始登录到 %s' % (self.hostaddr)
            ftp.login(self.username, self.password)
            print '成功登录到 %s' % (self.hostaddr)
            debug_print(ftp.getwelcome())
        except Exception:
            deal_error("连接或登录失败")
        try:
            print "now:",self.ftp.pwd()
            self.ftp.cwd(self.remotedir)
        except(Exception):
            deal_error('切换目录失败')
 
    def is_same_size(self, localfile, remotefile):
        try:
            remotefile_size = self.ftp.size(remotefile)
        except:
            remotefile_size = -1
        try:
            localfile_size = os.path.getsize(localfile)
        except:
            localfile_size = -1
        debug_print('lo:%d  re:%d' % (localfile_size, remotefile_size), )
        if remotefile_size == localfile_size:
            return 1
        else:
            return 0
 
    def download_file(self, localfile, remotefile):
        if self.is_same_size(localfile, remotefile):
            debug_print('%s 文件大小相同,无需下载' % localfile)
            return
        else:
            print "remotefile:",remotefile
            debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % localfile)
            # return
        file_handler = open(localfile, 'wb')
        self.ftp.retrbinary('RETR %s' % (remotefile), file_handler.write)
        file_handler.close()
 
    def download_files(self, localdir='./', remotedir='./'):
        try:
            print "remotedir:",remotedir
            self.ftp.cwd(remotedir)
        except:
            debug_print('目录%s不存在,继续...' % remotedir)
            return
        if not os.path.isdir(localdir):
            # pass
            os.makedirs(localdir)
        debug_print('切换至目录 %s' % self.ftp.pwd())
        self.file_list = []
        print(self.ftp.dir())
        self.ftp.dir(self.get_file_list)
        remotenames = self.file_list
        # print(remotenames)
        # return
        for item in remotenames:
            filetype = item[0]
            filename = item[1]
            print "filename:",filename
            local = os.path.join(localdir, filename).replace('\\', '/')
 
            if filetype == 'd':
                self.download_files(local, filename)
            elif filetype == '-':
                self.download_file(local, filename)
        self.ftp.cwd('..')
        debug_print('返回上层目录 %s' % self.ftp.pwd())
 
    def upload_file(self, localfile, remotefile):
        if not os.path.isfile(localfile):
            return
        if self.is_same_size(localfile, remotefile):
            debug_print('跳过[相等]: %s' % localfile)
            return
        file_handler = open(localfile, 'rb')
        self.ftp.storbinary('STOR %s' % remotefile, file_handler)
        file_handler.close()
        debug_print('已传送: %s' % localfile)
 
    def upload_files(self, localdir='./', remotedir='./'):
        if not os.path.isdir(localdir):
            return
        localnames = os.listdir(localdir)
        self.ftp.cwd(remotedir)
        for item in localnames:
            src = os.path.join(localdir, item)
            if os.path.isdir(src):
                try:
                    self.ftp.mkd(item)
                except:
                    debug_print('目录已存在 %s' % item)
                self.upload_files(src, item)
            else:
                self.upload_file(src, item)
        self.ftp.cwd('..')
 
    def get_file_list(self, line):
        print "line1:", line
        ret_arr = []
        file_arr = self.get_filename(line)
        print "file_arr:",file_arr
        if file_arr[1not in ['.''..']:
            self.file_list.append(file_arr)
 
    def get_filename(self, line):
        print "line2:",line
        print type(line)
        pos = line.rfind(':')
        while (line[pos] != ' '):
            pos += 1
        while (line[pos] == ' '):
            pos += 1
        print pos
        file_arr = [line[0], line[pos:]]
        return file_arr
 
 
def debug_print(s):
    print (s)
 
 
def deal_error(e):
    timenow = time.localtime()
    datenow = time.strftime('%Y-%m-%d', timenow)
    logstr = '%s 发生错误: %s' % (datenow, e)
    debug_print(logstr)
    file.write(logstr)
    sys.exit()
 
 
if __name__ == '__main__':
    file = open("log.txt""a")
    timenow = time.localtime()
    datenow = time.strftime('%Y-%m-%d', timenow)
    logstr = datenow
    # 配置如下变量
    hostaddr = '192.168.1.100'  # ftp地址
    username = 'ftpuser3'  # 用户名
    password = 'test1passwd'  # 密码
 
 
    port = 21  # 端口号
    #rootdir_local = '.' + os.sep + 'bak/'  # 本地目录
    rootdir_local = 'D:/ftp/'
    rootdir_remote = './'  # 远程目录
 
    = MYFTP(hostaddr, username, password, rootdir_remote, port)
    f.login()
    f.download_files(rootdir_local, rootdir_remote)
 
    timenow = time.localtime()
    datenow = time.strftime('%Y-%m-%d', timenow)
    logstr += " - %s 成功执行了备份\n" % datenow
    debug_print(logstr)
 
    file.write(logstr)
    file.close()



本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1907740,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Python编程:获取ftp目录下文件夹和文件
Python编程:获取ftp目录下文件夹和文件
695 0
python实现ftp服务端和客户端
python实现ftp服务端和客户端
204 0
Python开发基础总结(七)数据库+FTP+字符编码+源码安全
Python开发基础总结(七)数据库+FTP+字符编码+源码安全
146 0
python——脚本实现检测目标ip是否存在ftp笑脸漏洞(ftp后门漏洞)
vsftpd2.3.4中在6200端口存在一个shell,使得任何人都可以进行连接,并且VSFTPD v2.3.4 服务,是以 root 权限运行的,最终我们提到的权限也是root;当连接带有vsftpd 2.3.4版本的服务器的21端口时,输入用户中带有“:) ”,密码任意,因此也称为笑脸漏洞。
219 0
用 Python 快速实现 HTTP 和 FTP 服务器
用 Python 快速实现 HTTP 服务器有时你需临时搭建一个简单的 Web Server,但你又不想去安装 Apache、Nginx 等这类功能较复杂的 HTTP 服务程序时。这时可以使用 Python 内建的 SimpleHTTPServer 模块快速搭建一个简单的 HTTP 服务器。
3547 0
+关注
技术小阿哥
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
双剑合璧-Python和大数据计算平台的结合
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多