本文实例为大家分享了python3实现ftp服务功能的具体代码,供大家参考,具体内容如下
客户端 main代码:
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#Author by Andy #_*_ coding:utf-8 _*_ ''' This program is used to create a ftp client ''' import socket,os,json,time,hashlib,sys class Ftp_client( object ): def __init__( self ): self .client = socket.socket() def help ( self ): msg = '''useage: ls pwd cd dir(example: / .. . /var) put filename rm filename get filename mkdir directory name ''' print (msg) def connect( self ,addr,port): self .client.connect((addr,port)) def auth( self ): m = hashlib.md5() username = input ( "请输入用户名:" ).strip() m.update( input ( "请输入密码:" ).strip().encode()) password = m.hexdigest() user_info = { 'action' : 'auth' , 'username' :username, 'password' :password} self .client.send(json.dumps(user_info).encode( 'utf-8' )) server_response = self .client.recv( 1024 ).decode() # print(server_response) return server_response def interactive( self ): while True : msg = input ( ">>>:" ).strip() if not msg: print ( "不能发送空内容!" ) continue cmd = msg.split()[ 0 ] if hasattr ( self ,cmd): func = getattr ( self ,cmd) func(msg) else : self . help () continue def put( self , * args): cmd_split = args[ 0 ].split() if len (cmd_split) > 1 : filename = cmd_split[ 1 ] if os.path.isfile(filename): filesize = os.stat(filename).st_size file_info = { "action" : "put" , "filename" :filename, "size" :filesize, "overriding" : 'True' } self .client.send( json.dumps(file_info).encode( 'utf-8' ) ) #防止粘包,等待服务器确认。 request_code = { '200' : 'Ready to recceive data!' , '210' : 'Not ready to received data!' } server_response = self .client.recv( 1024 ).decode() if server_response = = '200' : f = open (filename, "rb" ) send_size = 0 start_time = time.time() for line in f: self .client.send(line) send_size + = len (line) send_percentage = int ((send_size / filesize) * 100 ) while True : progress = ( '\r已上传%sMB(%s%%)' % ( round (send_size / 102400 , 2 ), send_percentage)).encode( 'utf-8' ) os.write( 1 , progress) sys.stdout.flush() time.sleep( 0.0001 ) break else : end_time = time.time() time_use = int (end_time - start_time) print ( "\nFile %s has been sent successfully!" % filename) print ( '\n平均下载速度%s MB/s' % ( round ( round (send_size / 102400 , 2 ) / time_use, 2 ))) f.close() else : print ( "Sever isn't ready to receive data!" ) time.sleep( 10 ) start_time = time.time() f = open (filename, "rb" ) send_size = 0 for line in f: self .client.send(line) send_size + = len (line) # print(send_size) while True : send_percentage = int ((send_size / filesize) * 100 ) progress = ( '\r已上传%sMB(%s%%)' % ( round (send_size / 102400 , 2 ), send_percentage)).encode( 'utf-8' ) os.write( 1 , progress) sys.stdout.flush() # time.sleep(0.0001) break else : end_time = time.time() time_use = int (end_time - start_time) print ( "File %s has been sent successfully!" % filename) print ( '\n平均下载速度%s MB/s' % ( round ( round (send_size / 102400 , 2 ) / time_use, 2 ))) f.close() else : print ( "File %s is not exit!" % filename) else : self . help () def ls( self , * args): cmd_split = args[ 0 ].split() # print(cmd_split) if len (cmd_split) > 1 : path = cmd_split[ 1 ] elif len (cmd_split) = = 1 : path = '.' request_info = { 'action' : 'ls' , 'path' : path } self .client.send(json.dumps(request_info).encode( 'utf-8' )) sever_response = self .client.recv( 1024 ).decode() print (sever_response) def pwd( self , * args): cmd_split = args[ 0 ].split() if len (cmd_split) = = 1 : request_info = { 'action' : 'pwd' , } self .client.send(json.dumps(request_info).encode( "utf-8" )) server_response = self .client.recv( 1024 ).decode() print (server_response) else : self . help () def get( self , * args): cmd_split = args[ 0 ].split() if len (cmd_split) > 1 : filename = cmd_split[ 1 ] file_info = { "action" : "get" , "filename" : filename, "overriding" : 'True' } self .client.send(json.dumps(file_info).encode( 'utf-8' )) server_response = self .client.recv( 1024 ).decode() #服务器反馈文件是否存在 self .client.send( '0' .encode( 'utf-8' )) if server_response = = '0' : file_size = int ( self .client.recv( 1024 ).decode()) # print(file_size) self .client.send( '0' .encode( 'utf-8' )) #确认开始传输数据 if os.path.isfile(filename): filename = filename + '.new' f = open (filename, 'wb' ) receive_size = 0 m = hashlib.md5() start_time = time.time() while receive_size < file_size: if file_size - receive_size > 1024 : # 还需接收不止1次 size = 1024 else : size = file_size - receive_size data = self .client.recv(size) m.update(data) receive_size + = len (data) data_percent = int ((receive_size / file_size) * 100 ) f.write(data) progress = ( '\r已下载%sMB(%s%%)' % ( round (receive_size / 102400 , 2 ),data_percent)).encode( 'utf-8' ) os.write( 1 ,progress) sys.stdout.flush() time.sleep( 0.0001 ) else : end_time = time.time() time_use = int (end_time - start_time) print ( '\n平均下载速度%s MB/s' % ( round ( round (receive_size / 102400 , 2 ) / time_use, 2 ))) Md5_server = self .client.recv( 1024 ).decode() Md5_client = m.hexdigest() print ( '文件校验中,请稍候...' ) time.sleep( 0.3 ) if Md5_server = = Md5_client: print ( '文件正常。' ) else : print ( '文件与服务器MD5值不符,请确认!' ) else : print ( 'File not found!' ) client.interactive() else : self . help () def rm( self , * args): cmd_split = args[ 0 ].split() if len (cmd_split) > 1 : filename = cmd_split[ 1 ] request_info = { 'action' : 'rm' , 'filename' : filename, 'prompt' : 'Y' } self .client.send(json.dumps(request_info).encode( "utf-8" )) server_response = self .client.recv( 10240 ).decode() request_code = { '0' : 'confirm to deleted' , '1' : 'cancel to deleted' } if server_response = = '0' : confirm = input ( "请确认是否真的删除该文件:" ) if confirm = = 'Y' or confirm = = 'y' : self .client.send( '0' .encode( "utf-8" )) print ( self .client.recv( 1024 ).decode()) else : self .client.send( '1' .encode( "utf-8" )) print ( self .client.recv( 1024 ).decode()) else : print ( 'File not found!' ) client.interactive() else : self . help () def cd( self , * args): cmd_split = args[ 0 ].split() if len (cmd_split) > 1 : path = cmd_split[ 1 ] elif len (cmd_split) = = 1 : path = '.' request_info = { 'action' : 'cd' , 'path' :path } self .client.send(json.dumps(request_info).encode( "utf-8" )) server_response = self .client.recv( 10240 ).decode() print (server_response) def mkdir( self , * args): request_code = { '0' : 'Directory has been made!' , '1' : 'Directory is aleady exist!' } cmd_split = args[ 0 ].split() if len (cmd_split) > 1 : dir_name = cmd_split[ 1 ] request_info = { 'action' : 'mkdir' , 'dir_name' : dir_name } self .client.send(json.dumps(request_info).encode( "utf-8" )) server_response = self .client.recv( 1024 ).decode() if server_response = = '0' : print ( 'Directory has been made!' ) else : print ( 'Directory is aleady exist!' ) else : self . help () # def touch(self,*args): def run(): client = Ftp_client() # client.connect('10.1.2.3',6969) Addr = input ( "请输入服务器IP:" ).strip() Port = int ( input ( "请输入端口号:" ).strip()) client.connect(Addr,Port) while True : if client.auth() = = '0' : print ( "Welcome....." ) client.interactive() break else : print ( "用户名或密码错误!" ) continue |
目录结构:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。