Django 프로젝트에서pyftpdlib은 FTP 서버를 신속하게 구축하고 동적 증량으로 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django의 질문 및 답변 웹사이트환영 친구, 이것은 우리의 새로운 블로그입니다. 이 블로그에서는 , 과 같은 Question-n-Answer 웹사이트를 만들고 있습니다. 이 웹사이트는 회원가입 및 로그인이 가능합니다. 로그인 후 사용자는 사용자의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.