-
Notifications
You must be signed in to change notification settings - Fork 0
/
07ftp_client.py
60 lines (47 loc) · 1.41 KB
/
07ftp_client.py
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
"""
Author: TangYue
"""
'''
建立一个FTP服务器(本机目录)
下载FTP目录中一个随机文件
然后邮件发送给自己的邮箱(文件以附件的形式发送)
接收邮件,将刚才发送的邮件接收(包括附件)下来,邮件内容保存到本地(包括附件)
'''
import ftplib
import os
def connect(client):
serverIp = "127.0.0.1"
serverPort = 33
client.connect(serverIp, serverPort)
return client
def login(client):
username = "user"
password = "123456"
client.login(username, password)
return client
def list_file(client):
client.dir()
fileList = client.nlst()
return fileList
def download_file(client, fileList, parentPath=""):
remotePath = "./ftp/09flaskr.py.txt"
localPath = "./download"
if not os.path.exists(localPath):
os.makedirs(localPath)
localDir = "%s/ftp/09flaskr.py.txt" % localPath
fileHandler = open(localDir, "wb")
cmd = "RETR %s" % remotePath
print("\n\n")
print("download %s to %s" % (remotePath, localDir))
ret = client.retrbinary(cmd, fileHandler.write, 1024)
print(ret)
def client():
ftpClient = ftplib.FTP()
ftpClient = connect(ftpClient)
ftpClient = login(ftpClient)
print(ftpClient.welcome)
fileList = list_file(ftpClient)
return ftpClient, fileList
if __name__ == "__main__":
ftpClient, fileList = client()
download_file(ftpClient, fileList)