python-3.xftpftplibvsftpdvesta

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


I am trying to connect to FTP using Python ftplib but it shows connection was successful but each time I run any server command it fails with TimeoutError. NOOP command shows 200 NOOP ok. response but when I try ftp.dir() or any server command it fails with following error.

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)

I tried with ftplib.FTP() as well, shows same error.

Here is the vestacp vsftpd configuration:

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

Solution

  • I have changed pam_service_name from vsftpd to ftp and added pasv_address in the vsftpd configuration file.

    pam_service_name=ftp
    pasv_address=public_ip_address_of_server
    

    And following code worked after vsftpd restart.

    >>> 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