Python FTP服务器

#-*- coding:utf-8 -*-
# pip install pyftpdlib
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import os
# import getpass

def getIP():
    '''
    通过执行命令:ipconfig,获取当前电脑的IP地址
    '''
    con = os.popen('ipconfig').read()
    lines = con.split('
')
    for line in lines:
        if 'IPv4 地址' in line or 'IPv4 Address' in line:
            ip = line.split()[-1]
            return ip

def ftpServer(ip,port,name,password,path,permission):
    print('=========================== Address:  ftp://%s:%s  =======================
' % (ip, port))
    #实例化虚拟用户,这是FTP验证首要条件
    authorizer = DummyAuthorizer()

    #添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限)
    authorizer.add_user(name, password, path, perm=permission)

    # #添加匿名用户,赋予匿名用户可以访问的目录
    # authorizer.add_anonymous('E:/')

    #初始化ftp句柄
    handler = FTPHandler
    handler.authorizer = authorizer
    
    # 被动连接模式
    #handler.passive_ports = range(60000, 65535)

    #监听ip 和 端口
    server = FTPServer((ip,port), handler)
    
     # 设置连接限制
    # server.max_cons = 256
    # server.max_cons_per_ip = 5

    #开始服务
    server.serve_forever()

if __name__ == '__main__':
    ip = getIP()
    path = input('Path:')
    if path == '':
        path = os.getcwd()+'\'+'FTP'
        if not os.path.exists(path):
            os.mkdir(path)
    # 默认使用8888端口
    port = 8888

    # 可以隐藏输入的密码
    # password = getpass.getpass('Password:')
    ftpServer(ip,port,name='wzt',password='wztshine',path=path,permission='elradfmwMT')

        
'''    备注:权限列表
       Read permissions:
         - "e" = change directory (CWD command)
         - "l" = list files (LIST, NLST, STAT, MLSD, MLST, SIZE, MDTM commands)
         - "r" = retrieve file from the server (RETR command)

        Write permissions:
         - "a" = append data to an existing file (APPE command)
         - "d" = delete file or directory (DELE, RMD commands)
         - "f" = rename file or directory (RNFR, RNTO commands)
         - "m" = create directory (MKD command)
         - "w" = store a file to the server (STOR, STOU commands)
         - "M" = change file mode (SITE CHMOD command)
         - "T" = update file last modified time (MFMT command)
         '''
原文地址:https://www.cnblogs.com/wztshine/p/12116807.html