import ftplib
import time
def test_ftp_connection(host, username='', password=''):
start_time = time.time() # 记录开始时间
try:
# 尝试连接到FTP服务器
ftp = ftplib.FTP(host)
ftp.login(user=username, passwd=password) # 登录
ftp.quit() # 退出
end_time = time.time() # 记录结束时间
connection_time = end_time - start_time # 计算连接时间
print(f"成功连接到 {host},连接时间: {connection_time:.2f} 秒")
except ftplib.all_errors as e:
print(f"连接失败: {e}")
# 示例使用
if __name__ == "__main__":
host = "" # 替换为你的FTP服务器地址
username = "" # 替换为你的用户名(如果需要)
password = "" # 替换为你的密码(如果需要)
test_ftp_connection(host, username, password)
用python测试连接ftp服务器的速度发现要5s多
通过打开/etc/vsftpd/vsftpd.conf添加如下配置:
reverse_lookup_enable=NO
重启ftp服务器后重新测试连接速度: