FTP Server 基本代码

ftp.py

import os
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

current_dir = os.getcwd()


#--------------------------------------------------------------
#  FTP Server Config
#--------------------------------------------------------------

#--------
# DummyAuthorizer - 使用者权限配置
#--------

# Instantiate a dummy authorizer for managing 'virtual' users
authorizer = DummyAuthorizer()

#--------
# USER - 用户定义【匿名、一般】
#--------
# Define a new user having full r/w permissions and a read-only

# general user
# authorizer.add_user('mvlg', '123456', 'D:\My_files', perm='elradfmwMT')

# anonymous user
authorizer.add_anonymous('D:\My_files')

#--------
# FTP Handler - FTP 服务器处理配置
#--------
# Instantiate FTP handler class
handler = FTPHandler
handler.authorizer = authorizer

# Define a customized banner (string returned when client connects)
handler.banner = "pyftpdlib based ftpd ready."

# Specify a masquerade address and the range of ports to use for
# passive connections.  Decomment in case you're behind a NAT.
#handler.masquerade_address = '151.25.42.11'
#handler.passive_ports = range(60000, 65535)

#--------
# FTP SERVER - 具体FTP服务器配置
#--------
# Instantiate FTP server class and listen on 0.0.0.0:2121
address = ('192.168.2.126', 2121)
server = FTPServer(address, handler)

# set a limit for connections
# server.max_cons = 256
# server.max_cons_per_ip = 5

# start ftp server
server.serve_forever()

Run FTP Server.bat

python ftp.py
pause

你可能感兴趣的:(FTP Server 基本代码)