Django 프로젝트에서pyftpdlib은 FTP 서버를 신속하게 구축하고 동적 증량으로 ftp 사용자를 추가합니다

5521 단어 django
1. django에서 ftp 서비스 시작
#ftpServer.py
import os
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer, MultiprocessFTPServer, ThreadedFTPServer
from django.core.management import BaseCommand

class Command(BaseCommand):
    def handle(self, *args, **options):
        """
              ,  ftp  
        :param args:
        :param options:
        :return:
        """
        authorizer = DummyAuthorizer()
        handler = MyHandler
        handler.authorizer = authorizer
        ftp_server(authorizer)
        # server = FTPServer(("0.0.0.0", 2121), handler)
        # server = MultiprocessFTPServer(("0.0.0.0", 2121), handler)
        server = ThreadedFTPServer((FTP_IP, FTP_POST), handler)
        server.serve_forever()

1. 파일 저장 경로: apps//management/commands/ftpServer.py

2. ftp :python manage.py ftpServer

 

、 ftp

user_cfg_path = os.path.join(BASE_DIR + '/apps/ftpUser.cfg')
class MyHandler(FTPHandler):
    """
        
    """
    def on_connect(self):  #      
        ftp_server(self.authorizer)
        print("%s:%s connected" % (self.remote_ip, self.remote_port))


def get_user(userfile):
    #         
    user_list = []
    with open(userfile) as f:
        for line in f:
            if not line.startswith('#') and line:
                if len(line.split()) == 6:
                    # print(line.split())
                    user_list.append(line.split())
                else:
                    print("user.conf    ")
    return user_list


def ftp_server(authorizer):
    #          ,       (   ,   ,     ,   )
    # authorizer.add_user('user', '12345', '/home/', perm='elradfmw')
    user_list = get_user(user_cfg_path)
    for user in user_list:
        name, passwd, permit, homedir, date, time = user
        try:
            authorizer.add_user(name, passwd, homedir, perm=permit)
        except Exception as e:
            print(e)

class Command(BaseCommand):
    def handle(self, *args, **options):
        """
              ,  ftp  
        :param args:
        :param options:
        :return:
        """
        authorizer = DummyAuthorizer()
        handler = MyHandler
        handler.authorizer = authorizer
        ftp_server(authorizer)
        # server = FTPServer(("0.0.0.0", 2121), handler)
        # server = MultiprocessFTPServer(("0.0.0.0", 2121), handler)
        server = ThreadedFTPServer((FTP_IP, FTP_POST), handler)
        server.serve_forever()

: ftp , , ,

, ,

1.  :             ( )         

#ftpUser.cfg
[email protected]  SNFSTP  elr  /Users/cagey/gitlib_projects/test/PTM_backend/upload/files/[email protected]  2019-08-01 17:49:03.727385
[email protected]  P4X4R1  elr  /Users/cagey/gitlib_projects/test/PTM_backend/upload/files/[email protected]  2019-08-01 17:48:40
[email protected]  PYUVTN  elr  /Users/cagey/gitlib_projects/test/PTM_backend/upload/files/  2019-07-26 16:52:45
[email protected]  BB1IVP  elr  /Users/cagey/gitlib_projects/test/PTM_backend/upload/files/  2019-07-26 16:05:48
admin  123  elradfmwMT  /Users/cagey/gitlib_projects/test/PTM_backend/upload/files/  2019-07-26 15:54:00

2.  

def get_ftp_user(username):
    """
        ftp    
    :param username: [email protected]
    :return: ['[email protected]', 'JG14CD', 'elr', '/Users/cagey/gitlib_projects/tool/new/PTM_backend/upload/files/']
    """
    name = username
    with open(user_cfg_path) as f:
        for line in f:
            if line.startswith(name):
                if len(line.split()) == 6:
                    return line.split()
                else:
                    return "user.conf    !"
        return None


def del_ftp_user(username):
    """
      ftp  
    :param username:
    :return:
    """
    with open(user_cfg_path, 'r') as r:
        lines = r.readlines()
    with open(user_cfg_path, 'w') as w:
        for l in lines:
            if not l.startswith(username):
                w.write(l)


def add_ftp_user(user_info):
    """
      ,  ftp  
    :param user_info:
    :return:
    """
    with open(user_cfg_path, 'r') as fp:
        lines = []
        for line in fp:
            lines.append(line)
    with open(user_cfg_path, 'w') as fp:
        lines.insert(0, str(user_info) + '
') # s = ''.join(lines) fp.write(s) def random_code(): # 6 code = [] for i in range(6): if i == random.randint(1, 5): code.append(str(random.randint(1, 5))) else: code.append(chr(random.randint(65, 90))) return ''.join(code)

, pyftpdlib ,
 

 :https://blog.csdn.net/Cagey77/article/details/98176175

좋은 웹페이지 즐겨찾기