0

我正在尝试使用 Python 连接到 FTP,ftplib但它显示连接成功,但每次我运行任何服务器命令时,它都会以TimeoutError. NOOP命令显示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\38\lib\ftplib.py", line 542, in nlst
    self.retrlines(cmd, files.append)
  File "c:\program files\python\38\lib\ftplib.py", line 451, in retrlines
    with self.transfercmd(cmd) as conn, \
  File "c:\program files\python\38\lib\ftplib.py", line 382, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "c:\program files\python\38\lib\ftplib.py", line 781, in ntransfercmd
    conn, size = FTP.ntransfercmd(self, cmd, rest)
  File "c:\program files\python\38\lib\ftplib.py", line 343, in ntransfercmd
    conn = socket.create_connection((host, port), self.timeout,
  File "c:\program files\python\38\lib\socket.py", line 808, in create_connection
    raise err
  File "c:\program files\python\38\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\38\lib\ftplib.py", line 558, in dir
    self.retrlines(cmd, func)
  File "c:\program files\python\38\lib\ftplib.py", line 451, in retrlines
    with self.transfercmd(cmd) as conn, \
  File "c:\program files\python\38\lib\ftplib.py", line 382, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "c:\program files\python\38\lib\ftplib.py", line 781, in ntransfercmd
    conn, size = FTP.ntransfercmd(self, cmd, rest)
  File "c:\program files\python\38\lib\ftplib.py", line 343, in ntransfercmd
    conn = socket.create_connection((host, port), self.timeout,
  File "c:\program files\python\38\lib\socket.py", line 808, in create_connection
    raise err
  File "c:\program files\python\38\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\38\lib\ftplib.py", line 558, in dir
    self.retrlines(cmd, func)
  File "c:\program files\python\38\lib\ftplib.py", line 450, in retrlines
    resp = self.sendcmd('TYPE A')
  File "c:\program files\python\38\lib\ftplib.py", line 275, in sendcmd
    return self.getresp()
  File "c:\program files\python\38\lib\ftplib.py", line 238, in getresp
    resp = self.getmultiline()
  File "c:\program files\python\38\lib\ftplib.py", line 224, in getmultiline
    line = self.getline()
  File "c:\program files\python\38\lib\ftplib.py", line 206, in getline
    line = self.file.readline(self.maxline + 1)
  File "c:\program files\python\38\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
  File "c:\program files\python\38\lib\ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "c:\program files\python\38\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(),显示同样的错误。

  • ftplib与其他非 SSL 服务器一起工作正常。
  • 远程服务器正在运行Ubuntu 18.04.4 LTSvsftpd
  • FTP用户是用vestacp.
  • 我通过更改/etc/passwd文件更改了用户的家庭住址。
  • 一切正常,FileZilla没有来自同一本地服务器的任何延迟。

这是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
4

1 回答 1

0

pam_service_name已从vsftpdto更改ftp并添加pasv_addressvsftpd配置文件中。

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
于 2020-07-28T11:07:00.983 回答