[TOC]
python之FTP小工具
- 源码
#!/usr/bin/python3
import os
import sys
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler, ThrottledDTPHandler
from pyftpdlib.servers import FTPServer
import logging
def main(username, password, port, user_dir):
# 创建一个用户授权管理器
authorizer = DummyAuthorizer()
# 添加一个用户,用户名为传入的username,密码为传入的password,用户目录为传入的user_dir,权限为读写
authorizer.add_user(username, password, user_dir, perm="elradfmw")
# 添加一个匿名用户,用户目录为用户主目录下的"nobody"文件夹,权限为只读
nobody_dir = os.path.join(user_dir, "nobody")
if not os.path.exists(nobody_dir):
os.mkdir(nobody_dir)
authorizer.add_anonymous(nobody_dir, perm="elr")
# 初始化FTP处理器并设置用户授权管理器
handler = FTPHandler
handler.authorizer = authorizer
# handler.masquerade_address = nat_ip
# 设置上传和下载速度限制(单位:字节/秒)
dtp_handler = ThrottledDTPHandler
dtp_handler.read_limit = 30720000
dtp_handler.write_limit = 30720000
handler.dtp_handler = dtp_handler
# 创建FTP服务器并绑定到指定地址和端口
server = FTPServer(("0.0.0.0", port), handler)
# 配置日志记录
logging.basicConfig(filename='ftp.log', level=logging.INFO)
# 启动FTP服务器
server.serve_forever()
if __name__ == "__main__":
if len(sys.argv) != 5:
print("============ python local network ftp server =====================")
print("Usage: script in parameter <username> <password> <port> <ftp_dir>")
print("==================================================================")
sys.exit(1)
username = sys.argv[1]
password = sys.argv[2]
port = int(sys.argv[3])
user_dir = sys.argv[4]
main(username, password, port, user_dir)
- 使用方法
[devops@my-dev pyftp]$ ./pyftp
PyInstaller/loader/pyimod03_importers.py:495: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
============ python local network ftp server =====================
Usage: script in parameter <username> <password> <port> <ftp_dir>
==================================================================
[devops@my-dev pyftp]$ ./pyftp zhaosan 123456 10010 ./
[devops@my-dev pyftp]$ tail -100f ftp.log
INFO:pyftpdlib:concurrency model: async
INFO:pyftpdlib:masquerade (NAT) address: None
INFO:pyftpdlib:passive ports: None
INFO:pyftpdlib:>>> starting FTP server on 0.0.0.0:10010, pid=9185 <<<
INFO:pyftpdlib:concurrency model: async
INFO:pyftpdlib:masquerade (NAT) address: None
INFO:pyftpdlib:passive ports: None
INFO:pyftpdlib:>>> starting FTP server on 0.0.0.0:10010, pid=9185 <<<
INFO:pyftpdlib:127.0.0.1:45808-[] FTP session opened (connect)
INFO:pyftpdlib:127.0.0.1:45808-[zhaosan] USER 'zhaosan' logged in.
INFO:pyftpdlib:127.0.0.1:45808-[zhaosan] RETR /home/devops/Python/pyftp/pyftp.py completed=1 bytes=1937 seconds=0.001
INFO:pyftpdlib:127.0.0.1:45808-[zhaosan] CWD /home/devops/Python/pyftp/dist 250
INFO:pyftpdlib:127.0.0.1:45808-[zhaosan] STOR /home/devops/Python/pyftp/dist/pyftp.py completed=1 bytes=1937 seconds=0.001
INFO:pyftpdlib:127.0.0.1:45808-[zhaosan] FTP session closed (disconnect).
INFO:pyftpdlib:received interrupt signal
INFO:pyftpdlib:>>> shutting down FTP server, 1 socket(s), pid=9185 <<<
- 客户端登录使用
[root@my-dev ~]# ftp 127.0.0.1 10010
Connected to 127.0.0.1 (127.0.0.1).
220 pyftpdlib 1.5.9 ready.
Name (127.0.0.1:root): zhaosan
331 Username ok, send password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> dir
227 Entering passive mode (127,0,0,1,130,21).
125 Data connection already open. Transfer starting.
drwxrwxr-x 2 devops devops 4096 Feb 20 02:49 __pycache__
drwxrwxr-x 3 devops devops 4096 Feb 20 02:39 build
drwxrwxr-x 2 devops devops 4096 Feb 20 02:49 dist
-rw-rw-r-- 1 devops devops 5244 Feb 20 02:51 ftp.log
drwxrwxr-x 2 devops devops 4096 Feb 20 02:17 nobody
-rwxr-xr-x 1 devops devops 12483864 Feb 20 02:50 pyftp
-rwxrwxr-x 1 devops devops 1937 Feb 20 02:49 pyftp.py
-rw-rw-r-- 1 devops devops 996 Feb 20 02:49 pyftp.spec
226 Transfer complete.
ftp> get pyftp.py
local: pyftp.py remote: pyftp.py
227 Entering passive mode (127,0,0,1,134,239).
125 Data connection already open. Transfer starting.
226 Transfer complete.
1937 bytes received in 0.000178 secs (10882.02 Kbytes/sec)
ftp> ls
227 Entering passive mode (127,0,0,1,148,227).
125 Data connection already open. Transfer starting.
drwxrwxr-x 2 devops devops 4096 Feb 20 02:49 __pycache__
drwxrwxr-x 3 devops devops 4096 Feb 20 02:39 build
drwxrwxr-x 2 devops devops 4096 Feb 20 02:49 dist
-rw-rw-r-- 1 devops devops 5362 Feb 20 02:51 ftp.log
drwxrwxr-x 2 devops devops 4096 Feb 20 02:17 nobody
-rwxr-xr-x 1 devops devops 12483864 Feb 20 02:50 pyftp
-rwxrwxr-x 1 devops devops 1937 Feb 20 02:49 pyftp.py
-rw-rw-r-- 1 devops devops 996 Feb 20 02:49 pyftp.spec
226 Transfer complete.
ftp> cd dist
250 "/dist" is the current directory.
ftp> ls
227 Entering passive mode (127,0,0,1,151,168).
125 Data connection already open. Transfer starting.
-rwxr-xr-x 1 devops devops 12483864 Feb 20 02:49 pyftp
226 Transfer complete.
ftp> put pyftp.py
local: pyftp.py remote: pyftp.py
227 Entering passive mode (127,0,0,1,207,205).
125 Data connection already open. Transfer starting.
226 Transfer complete.
1937 bytes sent in 2.8e-05 secs (69178.57 Kbytes/sec)
ftp>