please ask, why ftp is slower than http server , it needs some special options ?
my same local network
python3 -m http.server 6789
speed 100M/s
python3 -m pyftpdlib -p 21 speed 25M/s
then i tried some options , it is slow always
# 定义FTP服务器地址和端口号
address = ("192.168.1.100", 21) # 监听所有接口的21端口
#server = FTPServer(address, handler)
server = ThreadedFTPServer(address, handler)
#server = MultiprocessFTPServer(address, handler)
# 设置被动模式地址和端口范围
server.passive_ports = range(1000, 2000)
server.max_cons = 100
server.max_cons_per_ip = 100
server.ac_in_buffer_size = 1 * 1024 * 1024 * 100
server.ac_out_buffer_size = 1 * 1024 * 1024 * 100
Perhaps it's Windows... This are the numbers I get on Linux:
$ python3 ftpclient.py
1.0s: 929.67 MB/s
2.0s: 1273.08 MB/s
3.0s: 1274.94 MB/s
4.0s: 941.45 MB/s
5.0s: 606.11 MB/s
6.0s: 1062.21 MB/s
7.0s: 1132.42 MB/s
8.0s: 1141.80 MB/s
9.0s: 1123.88 MB/s
10.0s: 1090.74 MB/s
11.0s: 1090.90 MB/s
12.0s: 1246.38 MB/s
13.0s: 1145.02 MB/s
14.0s: 1173.32 MB/s
15.0s: 1119.93 MB/s
16.0s: 1150.80 MB/s
17.0s: 1159.88 MB/s
18.0s: 1172.77 MB/s
19.0s: 1135.94 MB/s
The script:
import ftplib
import threading
import time
def download_ftp_file(host, user, passwd, remote_path, local_path):
ftp = ftplib.FTP()
ftp.connect(host, 2121)
ftp.login(user=user, passwd=passwd)
total_bytes = 0
start_time = time.time()
stop_flag = threading.Event()
def report_speed():
last_bytes = 0
while not stop_flag.is_set():
time.sleep(1)
elapsed = time.time() - start_time
current = total_bytes
delta = current - last_bytes
last_bytes = current
speed = delta / 1024 / 1024
print(f"{elapsed:5.1f}s: {speed:8.2f} MB/s")
def write_chunk(data):
nonlocal total_bytes
f.write(data)
total_bytes += len(data)
t = threading.Thread(target=report_speed)
t.start()
with open(local_path, "wb") as f:
ftp.retrbinary(f"RETR {remote_path}", write_chunk)
stop_flag.set()
t.join()
ftp.quit()
elapsed = time.time() - start_time
print(f"Downloaded {total_bytes} bytes in {elapsed:.2f}s "
f"({total_bytes / elapsed / 1024:.2f} KB/s avg)")
if __name__ == "__main__":
# Example usage.
download_ftp_file(
host="127.0.0.1",
user="anonymous",
passwd="anonymous@",
remote_path="win-10-2 Clone-disk1.vmdk",
local_path="example.txt",
)
@giampaolo thanks to reply.
I use you client script to test. It is also not well~
.. I test filezilla server 1.10.2 same on win10 to win10 , it is faster, I think the lib has some points to be good.
..
You're lucky. I've tried a lot on pyftpdlib but could only achieve a maximum FTP speed of 15 MB/s on a Windows Server in 1Gbps LAN environment. I think the description of pyftpdlib lacks info that it only works properly on Linux.