Python ftplib TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time

Python ftplib TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time

我正在尝试使用 Python ftplib 连接到 FTP 但它显示连接成功但每次我 运行 任何服务器命令都失败并显示 TimeoutErrorNOOP 命令显示 200 NOOP ok. 响应,但是当我尝试 ftp.dir() 或任何服务器命令时它失败并出现以下错误。

PyDev console: starting.
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> import ftplib
>>> ftp = ftplib.FTP_TLS()
>>> ftp.connect(host_address, timeout=999)
'220 (vsFTPd 3.0.3)'
>>> ftp.login(username, password)
'230 Login successful.'
>>> ftp.voidcmd("NOOP")
'200 NOOP ok.'
>>> ftp.nlst()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "c:\program files\python\lib\ftplib.py", line 542, in nlst
    self.retrlines(cmd, files.append)
  File "c:\program files\python\lib\ftplib.py", line 451, in retrlines
    with self.transfercmd(cmd) as conn, \
  File "c:\program files\python\lib\ftplib.py", line 382, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "c:\program files\python\lib\ftplib.py", line 781, in ntransfercmd
    conn, size = FTP.ntransfercmd(self, cmd, rest)
  File "c:\program files\python\lib\ftplib.py", line 343, in ntransfercmd
    conn = socket.create_connection((host, port), self.timeout,
  File "c:\program files\python\lib\socket.py", line 808, in create_connection
    raise err
  File "c:\program files\python\lib\socket.py", line 796, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
>>> ftp.dir()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "c:\program files\python\lib\ftplib.py", line 558, in dir
    self.retrlines(cmd, func)
  File "c:\program files\python\lib\ftplib.py", line 451, in retrlines
    with self.transfercmd(cmd) as conn, \
  File "c:\program files\python\lib\ftplib.py", line 382, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "c:\program files\python\lib\ftplib.py", line 781, in ntransfercmd
    conn, size = FTP.ntransfercmd(self, cmd, rest)
  File "c:\program files\python\lib\ftplib.py", line 343, in ntransfercmd
    conn = socket.create_connection((host, port), self.timeout,
  File "c:\program files\python\lib\socket.py", line 808, in create_connection
    raise err
  File "c:\program files\python\lib\socket.py", line 796, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
>>> ftp.set_pasv(False)
>>> ftp.dir()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "c:\program files\python\lib\ftplib.py", line 558, in dir
    self.retrlines(cmd, func)
  File "c:\program files\python\lib\ftplib.py", line 450, in retrlines
    resp = self.sendcmd('TYPE A')
  File "c:\program files\python\lib\ftplib.py", line 275, in sendcmd
    return self.getresp()
  File "c:\program files\python\lib\ftplib.py", line 238, in getresp
    resp = self.getmultiline()
  File "c:\program files\python\lib\ftplib.py", line 224, in getmultiline
    line = self.getline()
  File "c:\program files\python\lib\ftplib.py", line 206, in getline
    line = self.file.readline(self.maxline + 1)
  File "c:\program files\python\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
  File "c:\program files\python\lib\ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "c:\program files\python\lib\ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2607)

我也试过 ftplib.FTP(),显示同样的错误。

这里是 vestacp vsftpd 配置:

anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
anon_umask=022
anon_upload_enable=NO
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
dual_log_enable=YES
chroot_local_user=YES
listen=YES
pam_service_name=vsftpd
userlist_enable=NO
tcp_wrappers=YES
force_dot_files=YES
ascii_upload_enable=YES
ascii_download_enable=YES
allow_writeable_chroot=YES
seccomp_sandbox=NO
pasv_enable=YES
pasv_promiscuous=YES
pasv_min_port=12000
pasv_max_port=12100
max_per_ip=10
max_clients=100
use_localtime=YES
utf8_filesystem=YES

#ssl
ssl_enable=YES
allow_anon_ssl=NO
require_ssl_reuse=NO
ssl_ciphers=HIGH
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO
force_local_data_ssl=NO
force_local_logins_ssl=NO
rsa_cert_file=/usr/local/vesta/ssl/certificate.crt
rsa_private_key_file=/usr/local/vesta/ssl/certificate.key

我已将 pam_service_namevsftpd 更改为 ftp,并在 vsftpd 配置文件中添加了 pasv_address

pam_service_name=ftp
pasv_address=public_ip_address_of_server

以下代码在 vsftpd 重启后有效。

>>> ftps = ftplib.FTP_TLS(server_host_address)
>>> ftps.login(username, password)
'230 Login successful.'
>>> ftps.prot_p()
'200 PROT now Private.'
>>> ftps.dir()
drwxrwxr-x    2 1002     33         708608 Jul 24 03:29 test_dir