Python Socketserver implements FTP functionality

Source: Internet
Author: User

Demand:

    1. User encryption authentication
    2. Allow simultaneous multi-user logins
    3. Each user has their own home directory and can only access their home directory
    4. Disk quotas for users, with different free space per user
    5. Allow user to switch directories at random on FTP server
    6. Allow users to view files in the current directory
    7. Allows uploading and downloading of files to ensure file consistency
    8. Show progress bar during file transfer
    9. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.