Demand:
- User encryption authentication
- Allow simultaneous multi-user logins
- Each user has their own home directory and can only access their home directory
- Disk quotas for users, with different free space per user
- Allow user to switch directories at random on FTP server
- Allow users to view files in the current directory
- Allows uploading and downloading of files to ensure file consistency
- Show progress bar during file transfer
- Additional features: Support for file breakpoint continuation
Client
#coding: Utf-8import socketimport jsonimport os#client.connect ((' 192.168.16.200 ', 9999)) class FtpClient (object): def _ _init__ (self): Self.client = Socket.socket () def help (self): msg = "ls pwd CD. /.. Get filename put filename ' Print (msg) def connect (self,ip,port): Self.client.connect ((IP , Port)) def Interactive (self): #self. Authenticate () while true:cmd = input (">>"). Strip () If Len (cmd) ==0:continue cmd_str = Cmd.split () [0] if hasattr (self, "cmd_%s"% cmd_str): Func = GetAttr (self, "cmd_%s"% cmd_str) func (cmd) else:self.help () def cmd_put (Self,*args): Cmd_split = Args[0].split () If Len (cmd_split) >1:filename = C MD_SPLIT[1] If os.path.isfile (filename): filesize = os.stat (filename). st_size msg _dic = { "Action": "Put", "filename": filename, "size": filesize, "Overridden": True} self.client.send (Json.dumps (msg_dic). Encode ("Utf-8")) Print ("Send", Json.dumps (Msg_dic). Encode ("Utf-8")) #防止粘包, wait for the server to confirm Server_response = Self.cli ENT.RECV (1024x768) f = open (filename, "RB") for line in F:self.client.send (li NE) else:print ("File Upload success ...") F.close () Else: Print (filename, "isn't exist") def cmd_get (self): Passftp = FtpClient () ftp.connect ("localhost", 99 ) ftp.interactive ()
Server:
Import Socketserverimport json,osclass Mytcphandler (socketserver. Baserequesthandler): def put (Self,*args): ' Receive client file ' ' Cmd_dic = args[0] filename = cmd_dic["file Name "] FileSize = cmd_dic[" Size "] if os.path.isfile (filename): f = open (filename +". New "," WB ") else:f = open (filename, "WB") Self.request.send (b "OK") Received_size = 0 while Received_size < Filesize:data = Self.request.recv (1024x768) f.write (data) received_size + = Len (data) else:print ("file [%s] has uploaded ..."% filename) def handle (self): while True: Try:self.data = Self.request.recv (1024x768). Strip () print ("{} wrote:". Format (SELF.C Lient_address[0]) print (self.data) cmd_dic = Json.loads (Self.data.decode ()) Action = cmd_dic["action"] if Hasattr (self,action): Func = GetAttr (self,action) func (cmd_dic) except Connectionreseterror as E: Print ("Err", e) breakif __name__ = = "__main__": HOST, PORT = "localhost", 9999 # Create the server , binding to localhost on port 9999 server = Socketserver. Threadingtcpserver (HOST, PORT), Mytcphandler) Server.serve_forever ()
Python Socketserver implements FTP functionality