• python socketserver 实现 ftp功能


    需求:

    1. 用户加密认证
    2. 允许同时多用户登录
    3. 每个用户有自己的家目录 ,且只能访问自己的家目录
    4. 对用户进行磁盘配额,每个用户的可用空间不同
    5. 允许用户在ftp server上随意切换目录
    6. 允许用户查看当前目录下文件
    7. 允许上传和下载文件,保证文件一致性
    8. 文件传输过程中显示进度条
    9. 附加功能:支持文件的断点续传

    client:

    #coding:utf-8
    import socket
    import json
    import 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 = cmd_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") )
                    #防止粘包,等服务器确认
                    server_response = self.client.recv(1024)
                    f = open(filename,"rb")
                    for line in f:
                        self.client.send(line)
                    else:
                        print("file upload success...")
                        f.close()
    
                else:
                    print(filename,"is not exist")
        def cmd_get(self):
            pass
    
    
    ftp = FtpClient()
    ftp.connect("localhost",9999)
    ftp.interactive()
    

    server:

    import socketserver
    import json,os
    class MyTCPHandler(socketserver.BaseRequestHandler):
    
    
        def put(self,*args):
            '''接收客户端文件'''
            cmd_dic = args[0]
            filename = cmd_dic["filename"]
            filesize = cmd_dic["size"]
            if os.path.isfile(filename):
                f = open(filename + ".new","wb")
            else:
                f = open(filename , "wb")
    
            self.request.send(b"200 ok")
            received_size = 0
            while received_size < filesize:
                data = self.request.recv(1024)
                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(1024).strip()
                    print("{} wrote:".format(self.client_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)
                    break
    if __name__ == "__main__":
        HOST, PORT = "localhost", 9999
        # Create the server, binding to localhost on port 9999
        server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)
        server.serve_forever()
    
  • 相关阅读:
    leetcode练习:26. Remove Duplicates from Sorted Array
    leetcode练习:11. Container With Most Water
    leetcode练习:5. Longest Palindromic Substring
    leetcode练习:2.Add Two Numbers
    算法笔记:分治
    (排序回顾)快速排序
    (排序回顾)归并排序
    leetcode练习:2017/09/21~09/22
    算法笔记:递归&迭代
    在Treeview中节点的data属性中保存记录类型及其消除的办法
  • 原文地址:https://www.cnblogs.com/iexperience/p/9293642.html
Copyright © 2020-2023  润新知