欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python FTP服务器实现(Python3)

程序员文章站 2022-03-25 19:02:26
创建一个ftp.py文件(Linux环境),插入以下代码: from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyftpdlib.servers impor ......

创建一个ftp.py文件(linux环境),插入以下代码:

from pyftpdlib.authorizers import dummyauthorizer
from pyftpdlib.handlers import ftphandler
from pyftpdlib.servers import ftpserver

# 实例化dummyauthorizer来创建ftp用户
authorizer = dummyauthorizer()
# 参数:用户名,密码,目录,权限
authorizer.add_user('test', '12345', '/root/ftp', perm='elradfmwmt')
authorizer.add_user('tese1', '12345', '/root/ftp', perm='elradfmwmt')
# 匿名登录
# authorizer.add_anonymous('/home/nobody')

handler = ftphandler
handler.authorizer = authorizer

# 参数:ip,端口,handler
server = ftpserver(('172.31.8.88', 21), handler)
server.serve_forever()

###### 文件保存
:wq 保存文件

###### 运行:

root@k8s-node1 ~]# nohup python3 ftp.py &
[1] 1737
[root@k8s-node1 ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@k8s-node1 ~]# root@k8s-node1 ~]# tail -f nohup.out
[i 2018-11-30 12:52:03] 172.31.8.3:52635-[test] user 'test' logged in.
[i 2018-11-30 12:57:03] 172.31.8.3:52635-[test] control connection timed out.
[i 2018-11-30 12:57:03] 172.31.8.3:52635-[test] ftp session closed (disconnect).
[i 2018-12-20 09:36:26] >>> starting ftp server on 172.31.8.88:21, pid=1737 <<<
[i 2018-12-20 09:36:26] concurrency model: async
[i 2018-12-20 09:36:26] masquerade (nat) address: none
[i 2018-12-20 09:36:26] passive ports: none
[i 2018-12-20 09:36:49] 172.31.8.3:52150-[] ftp session opened (connect)
[i 2018-12-20 09:36:52] 172.31.8.3:52150-[] user 'anonymous' failed login.
[i 2018-12-20 09:36:52] 172.31.8.3:52150-[] ftp session closed (disconnect).
[i 2018-12-20 09:37:09] 172.31.8.3:52156-[] ftp session opened (connect)
[i 2018-12-20 09:37:12] 172.31.8.3:52156-[] user 'test' failed login.
[i 2018-12-20 09:37:12] 172.31.8.3:52156-[] ftp session closed (disconnect).
[i 2018-12-20 09:37:16] 172.31.8.3:52159-[] ftp session opened (connect)
[i 2018-12-20 09:37:16] 172.31.8.3:52159-[test] user 'test' logged in.
[i 2018-12-20 09:37:16] 172.31.8.3:52159-[test] ftp session closed (disconnect).
[i 2018-12-20 09:37:16] 172.31.8.3:52160-[] ftp session opened (connect)
[i 2018-12-20 09:37:16] 172.31.8.3:52160-[test] user 'test' logged in.
[i 2018-12-20 09:40:13] 172.31.8.3:52160-[test] ftp session closed (disconnect).