阿小信大人的头像
Talk is cheap. Show me the code. Linus Torvalds

python ftp 上传下载demo2015-09-14 17:34

 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
#!/usr/bin/env python
# encoding: utf-8
from ftplib import FTP


class Ftp(object):

    def __init__(self, host, user, passwd, **kwargs):
        self.ftp = FTP(host=host, user=user, passwd=passwd, **kwargs)
        print(self.ftp.getwelcome())

    def download(self, path, filename, saveas=None):
        self.ftp.cwd(path)
        if not saveas:
            saveas = filename
        with open(saveas, 'wb') as localfile:
            self.ftp.retrbinary('RETR ' + filename, localfile.write, 1024)
        print('download ' + filename)

    def upload(self, filename, path=None):
        if path:
            self.ftp.cwd(path)
        with open(filename, 'rb') as localfile:
            self.ftp.storbinary('STOR ' + filename, localfile)
        print('upload ' + filename)

    def ls(self, path):
        self.ftp.cwd(path)
        self.ftp.retrlines('LIST')

    def quit(self):
        self.ftp.quit()


if __name__ == '__main__':
    ftp = Ftp('localhost', 'user', 'passwd')
    ftp.ls('~')
    ftp.quit()

如果您觉得从我的分享中得到了帮助,并且希望我的博客持续发展下去,请点击支付宝捐赠,谢谢!

若非特别声明,文章均为阿小信的个人笔记,转载请注明出处。文章如有侵权内容,请联系我,我会及时删除。

#Python#  
分享到:
阅读[1163] 评论[0]

你可能也感兴趣的文章推荐

本文最近访客

发表评论