-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFTP_Client.py
122 lines (106 loc) · 3.15 KB
/
FTP_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
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
from socket import *
import sys
import time
# 具体功能
class FtpClient:
def __init__(self,sockfd):
self.sockfd = sockfd
def do_list(self):
self.sockfd.send(b'L') # 发送请求
# 等待回复
data = self.sockfd.recv(128).decode()
# ok表示请求成功
if data == 'OK':
# 接收文件列表
data = self.sockfd.recv(4096)
print(data.decode())
else:
print(data)
def do_quit(self):
self.sockfd.send(b'Q')
self.sockfd.close()
sys.exit("谢谢使用")
def do_get(self,filename):
# 发送请求
self.sockfd.send(('G '+filename).encode())
# 等待回复
data = self.sockfd.recv(128).decode()
if data == 'OK':
fd = open(filename,'wb')
# 接收内容写入文件
while True:
data = self.sockfd.recv(1024)
if data == b'##':
break
fd.write(data)
fd.close()
else:
print(data)
def do_put(self, filename):
try:
f = open(filename,'rb')
except Exception:
print("没有该文件")
return
# 发送请求
filename = filename.split('/')[-1]
self.sockfd.send(('P ' + filename).encode())
# 等待回复
data = self.sockfd.recv(128).decode()
if data == 'OK':
while True:
data = f.read(1024)
if not data:
time.sleep(0.1)
self.sockfd.send(b'##')
break
self.sockfd.send(data)
f.close()
else:
print(data)
# 发起请求
def request(sockfd):
ftp = FtpClient(sockfd)
while True:
print("\n==========命令选项===========")
print("*********** list ************")
print("********* get file *********")
print("********* put file *********")
print("*********** quit ************")
print("==============================")
cmd = input("输入命令:")
if cmd.strip() == 'list':
ftp.do_list()
elif cmd.strip() == 'quit':
ftp.do_quit()
elif cmd[:3] == 'get':
filename = cmd.strip().split(' ')[-1]
ftp.do_get(filename)
elif cmd[:3] == 'put':
filename = cmd.strip().split(' ')[-1]
ftp.do_put(filename)
# 网络链接
def main():
# 服务器地址
ADDR = ('127.0.0.1',8080)
sockfd = socket()
try:
sockfd.connect(ADDR)
except Exception as e:
print("链接服务器失败")
return
else:
print("""
************************
Data File Image
************************
""")
cls = input("请选择文件种类:")
if cls not in ['Data','File','Image']:
print("Sorry input Error!!")
return
else:
sockfd.send(cls.encode())
request(sockfd) # 发送具体请求
if __name__ == "__main__":
main()