Python3 implements ftp service functions (client) and python3ftp
The example in this article shares with you the code for implementing ftp service functions in python3 for your reference. The specific content is as follows:
Client main code:
# Author by Andy # _ * _ coding: UTF-8 _ * _ ''' This program is used to create an ftp client ''' import socket, OS, json, time, hashlib, sysclass 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 ("Enter the User name :"). strip () m. update (input ("Enter Password :"). 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 (1, 1024 ). decode () # print (server_response) return server_response def interactive (self): while Tru E: msg = input (">>:"). strip () if not msg: print (" Empty content cannot be sent! ") Continue cmd = msg. split () [0] if hasattr (self, cmd): func = getattr (self, cmd) func (msg) else: self. help () continue def put (self, * args): pai_split = args [0]. split () if len (cmd_split)> 1: filename = pai_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 (fil E_info). encode ('utf-8') # prevents packet sticking and waits for confirmation from the server. Request_code = {'000000': 'Ready to recceive data! ', '000000':' Not ready to received data! '} Server_response = self. client. recv (1, 1024 ). decode () if server_response = '000000': 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: SS = ('\ r uploaded % 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 average download speed % s MB/s' % (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) SS = ('\ r uploaded % 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 average download speed % s MB/s' % (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): pai_split = args [0]. split () # print (pai_split) if len (pai_split)> 1: path = pai_split [1] elif len (pai_split) = 1: path = '. 'request_info = {'action': 'Ls', 'path': path} self. client. send (json. dumps (request_info ). encode ('utf-8') sever_response = self. client. recv (1, 1024 ). decode () print (sever_response) def pwd (self, * args): pai_split = args [0]. spli T () if len (export _split) = 1: request_info = {'action': 'pwd',} self. client. send (json. dumps (request_info ). encode ("UTF-8") server_response = self. client. recv (1, 1024 ). decode () print (server_response) else: self. help () def get (self, * args): pai_split = args [0]. split () if len (pai_split)> 1: filename = pai_split [1] file_info = {"action": "get", "filename": filename, "overriding ": 'true'} self. client. se Nd (json. dumps (file_info ). encode ('utf-8') server_response = self. client. recv (1, 1024 ). decode () # Check whether the file contains self. client. send ('0 '. encode ('utf-8') if server_response = '0': file_size = int (self. client. recv (1, 1024 ). decode () # print (file_size) self. client. send ('0 '. encode ('utf-8') # confirm to start data transmission 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: # more than one 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 downloaded % sMB (% s %)' % (round (receive_size/102400,2), data_percent )). encode ('utf-8') OS. write (1, progre Ss) sys. stdout. flush () time. sleep (0.0001) else: end_time = time. time () time_use = int (end_time-start_time) print ('\ n average download speed % s MB/s' % (round (receive_size/102400,2)/time_use, 2) Md5_server = self. client. recv (1, 1024 ). decode () Md5_client = m. hexdigest () print ('file validation, please wait... ') time. sleep (0.3) if Md5_server = Md5_client: print ('the file is normal. ') Else: print (' the file does not match the server's MD5 value. Please confirm! ') Else: print ('file not found! ') Client. interactive () else: self. help () def rm (self, * args): pai_split = args [0]. split () if len (cmd_split)> 1: filename = pai_split [1] request_info = {'action': 'rm ', 'filename': filename, 'propt ': 'y'} self. client. send (json. dumps (request_info ). encode ("UTF-8") server_response = self. client. recv (1, 10240 ). decode () request_code = {'0': 'Confirm to deleted', '1': 'cel to deleted'} if server_respon Se = '0': confirm = input ("check whether the file is deleted:") if confirm = 'y' or confirm = 'y': self. client. send ('0 '. encode ("UTF-8") print (self. client. recv (1, 1024 ). decode () else: self. client. send ('1 '. encode ("UTF-8") print (self. client. recv (1, 1024 ). decode () else: print ('file not found! ') Client. interactive () else: self. help () def cd (self, * args): pai_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 (1, 10240 ). decode () print (server_response) def mkdir (self, * args): request_code = {'0': 'direct Ory has been made! ', '1': 'Directory is aleady exist! '} Pai_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 (1, 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 ("Enter the Server IP Address :"). strip () Port = int (input ("enter the Port number :"). strip () client. connect (Addr, Port) while True: if client. auth () = '0': print ("Welcome ..... ") client. interactive () break else: print ("incorrect user name or password! ") Continue
Directory structure:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.