(축적)python 의 pyftpdlib

pyftpdlib 모듈 의 document 주소:http://pythonhosted.org/pyftpdlib/ 시험 주소:https://www.shiyanlou.com/courses/725
코드 구현:
  baseftp.ini
  #   user    passwd  homedir         path
  #   neo     123456  /home/neo/data  elradfmwM   
  neo 123 /home/neo   elradfmwM
  abc 456 /tmp    elradfmwM
  qwe 789 /tmp   elradfmwM
  ========
  config_ftp.py
#!/bin/python3.5
# -*- coding:utf-8 -*-

ip = "0.0.0.0"
port = 21
max_upload = 300 * 1024
max_download = 300 * 1024
max_cons = 256
max_pre_ip = 10
passive_ports = (2223,2233)
enable_anonymous = False
enable_logging = True
logging_name = r"pyftp.log"
masquerade_address = ""
welcom_banner = r"Welcome to private ftp."
anonymous_path = r"/tmp"
    =======
#!/bin/python3.5
# -*- coding:utf-8 -*-
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.servers import FTPServer
from pyftpdlib.handlers import ThrottledDTPHandler
from pyftpdlib.handlers import FTPHandler
import logging
from config_ftp import *

def init_ftp_server():
    authorize = DummyAuthorizer()
    """
               :
             - "e" =       
             - "l" =      (LIST, NLST, STAT, MLSD, MLST, SIZE, MDTM commands)
             - "r" =          (RETR command)

               :
             - "a" =      (APPE command)
             - "d" =      (DELE, RMD commands)
             - "f" =       (RNFR, RNTO commands)
             - "m" =      (MKD command)
             - "w" =     (STOR, STOU commands)
             - "M" =        (SITE CHMOD command)
    """
    # #     
    if enable_logging:
        logging.basicConfig(filename="pyftp.log",level=logging.INFO)
    #     
    if enable_anonymous:
        authorize.add_anonymous(anonymous_path)
    #         
    for user in user_list:
        name, passwd, homedir, permit = user
        try:
            authorize.add_user(name, passwd, homedir, perm=permit)
        except:
            print("user info config error. %s", user[0])
            exit(2)

    #   ftp      
    dtl_handler = ThrottledDTPHandler
    dtl_handler.read_limit = max_download
    dtl_handler.write_limit = max_upload

    #        
    handler = FTPHandler
    handler.authorizer = authorize
    handler.banner = welcom_banner
    handler.passive_ports = passive_ports
    handler.masquerade_address = masquerade_address

    address_port = (ip,port)
    server = FTPServer(address_port,handler=handler)
    server.max_cons = max_cons
    server.max_cons_per_ip = max_pre_ip
    server.serve_forever()

def init_conf_file(text):
    for x,item in enumerate(text):
        if item == "#":
            return text[:x]
        pass
    return text

def init_user_listconf():   #
    try:
        f = open('baseftp.ini',encoding='utf-8')
    except:
        print ("baseftp open error.")
        exit(1)
    while 1:
        line = f.readline()
        if len(init_conf_file(line)) > 3:
            user_list.append(line.split())
        if not line:
            break
    f.close()

if __name__ == '__main__':
    user_list = []
    init_user_listconf()
    init_ftp_server()

실행 테스트:
  sudo python3.5 baseftp.py

이런 공식 문서 만 알 아 볼 수 있 고 많은 문서 들 이 알 아 볼 수 없다.

좋은 웹페이지 즐겨찾기