主要需求
简单, 文件少, 建立快速
用户少, 最好只有一个, 就是用来自己传文件的
开启ssl加密
开启被动模式
环境要求
建议在虚拟环境中建立
安装模块pyftpdlib和pyopenssl
文件结构
主文件: ftp.py, 见下
日志文件: log, 自动生成
证书密钥文件: crt_key.pem, 制作说明详见:《openssl生成pyftpdlib启用ftps所需文件》
上传文件的根目录: /rootftp/, 要自己新建好
范本代码
# coding:utf-8
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import TLS_FTPHandler
from pyftpdlib.servers import FTPServer
def main():
# Instantiate a dummy authorizer for managing 'virtual' users
authorizer = DummyAuthorizer()
# Define a new user having full r/w permissions and a read-only
# anonymous user
authorizer.add_user('zhangsan', '12345678', '/', perm='elradfmwMT')
# Instantiate FTP handler class
handler = TLS_FTPHandler
handler.authorizer = authorizer
# Define a customized banner (string returned when client connects)
handler.banner = "pyftpdlib(xxxxxxxxxx) 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 = '172.188.26.175'
handler.passive_ports = range(60000, 65535)
# ssl加密
# 导入private key + certification的pem文件(上半截是私钥,下半截是证书)
handler.certfile = 'crt_key.pem'
# requires SSL for both control and data channel 启用ssl加密传输
# handler.tls_control_required = True # 控制连接启用ssl加密
# 我个人的感觉是,如果是开了这个控制连接加密,那么FTP刷新或者打开目录时经常要卡顿,卡的时间稳定在21秒左右,提示的是[正在协商密码],如果把这个控制连接加密关掉,速度就快多了.
handler.tls_data_required = True # 数据连接启用ssl加密
# Instantiate FTP server class and listen on 0.0.0.0:2121
address = ('0.0.0.0', 2121)
server = FTPServer(address, handler)
# set a limit for connections
server.max_cons = 8
server.max_cons_per_ip = 8
# start ftp server
server.serve_forever()
if __name__ == '__main__':
main()
# nohup py -u ftp.py >> log 2>&1 &
需要自行修改的设置:
用户名, 密码, 路径, 权限; 上面写的是'zhangsan', '12345678', '/', perm='elradfmwMT'
伪IP地址, 上面写的是172.188.26.175
端口号, 上面写的是2121, 需要在安全组里放通该端口号