ftp/FtpServer.py
#!/usr/bin/env python
#coding:utf-8
import sys
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler, ThrottledDTPHandler
from pyftpdlib.servers import FTPServer
sys.path.append('/***/svn/python/service/ftp/conf')
import settings
import logging
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(",")) == 4:
user_list.append(line.split(","))
else:
print "user.conf配置错误"
return user_list
def ftp_server():
#实例化虚拟用户,这是FTP验证首要条件
authorizer = DummyAuthorizer()
#添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限)
#authorizer.add_user('user', '12345', '/***/tempfile/market', perm='elradfmw')
user_list = get_user('/***/svn/python/service/ftp/conf/user.py')
for user in user_list:
name, passwd, permit, homedir = user
try:
authorizer.add_user(name, passwd, homedir.replace("\r\n",""), perm=permit)
except Exception as e:
print e
#添加匿名用户 只需要路径
if settings.enable_anonymous == 'on':
authorizer.add_anonymous(settings.anonymous_path)
#下载上传速度设置
dtp_handler = ThrottledDTPHandler
dtp_handler.read_limit = settings.max_download
dtp_handler.write_limit = settings.max_upload
#初始化ftp句柄
handler = FTPHandler
handler.authorizer = authorizer
#日志记录
if settings.enable_logging == 'on':
logging.basicConfig(filename=settings.loging_name, level=logging.INFO)
#欢迎信息
handler.banner = settings.welcome_msg
#添加被动端口范围
handler.passive_ports = range(settings.passive_ports[0], settings.passive_ports[1])
#监听ip 和 端口
server = FTPServer((settings.ip, settings.port), handler)
#最大连接数
server.max_cons = settings.max_cons
server.max_cons_per_ip = settings.max_per_ip
#开始服务
print '开始服务'
server.serve_forever()
if __name__ == "__main__":
ftp_server()
ftp/conf/settings.py
#!/usr/bin/env python
#coding:utf-8
ip = '***'
port = '2121'
#上传速度 300kb/s
max_upload = 300 * 1024
#下载速度 300kb/s
max_download = 300 * 1024
#最大连接数
max_cons = 10
#最多IP数
max_per_ip = 5
#被动端口范围,注意被动端口数量要比最大IP数多,否则可能出现无法连接的情况
passive_ports = (2000, 2200)
#是否开启匿名访问 on|off
enable_anonymous = 'off'
#匿名用户目录
anonymous_path = '/***/tempfile/market'
#是否开启日志 on|off
enable_logging = 'off'
#日志文件
loging_name = 'pyftp.log'
#欢迎信息
welcome_msg = 'Welcome to our ftp'
ftp/conf/user.py
#!/usr/bin/env python
#coding:utf-8
#用户名,密码,权限,目录
tcc,tcc,elradfmwM,/***/tempfile/market
mark_ftp,dept_mark,elradfmwM,/***/tempfile/market
goupengdong,dept_mark,elradfmwM,/***/tempfile/market
website_dev,website_dev,elradfmwM,/***/tempfile/market/web
后台运行:
chmod -R 777 /***/tempfile/market
nohup python /***/svn/python/service/ftp/FtpServer.py &
more:http://www.jb51.net/article/110901.htm