1
votes

I have the following code to download from 4 different FTP servers. It works on my local machine, but on server "ftp4" is not connecting. It is weird that I can connect to "ftp4" with FileZilla without any problem on the server. Here is the code:

HOST_LIST = [["10.10.10.04", "user", "pasword"]] #ftp4

self.cnopts = pysftp.CnOpts()
self.cnopts.hostkeys = None
        
 def get_r_portable(self, sftp, remotedir, localdir, preserve_mtime=False):
    for entry in sftp.listdir(remotedir):
        remotepath = remotedir + "/" + entry
        localpath = os.path.join(localdir, entry)
        mode = sftp.stat(remotepath).st_mode
        if S_ISREG(mode):
            if self.PATHFILTER.strip() != "":
                 if str(remotepath).lower().find(self.PATHFILTER.lower()) > -1:
                    sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)
                    
              
for host in self.HOST_LIST:
            with pysftp.Connection(host[0], username=host[1], password=host[2], cnopts=self.cnopts) as sftp:
                try:
                    for dirs in self.FOL_LIST:
                        currdir = REMOTEFOLDER + dirs + ""
                        try:
                            self.get_r_portable(sftp, currdir, self.LOCALFOLDER, True)
                        except Exception as e:
                            self.logger.error("Exception in dir exploration" + str(e))
                except Exception as e:
                    print('error')

The error that I get for "ftp4":

[ 2020-03-08 15:05:03,574 ] [  ][ WARNING ] Please note that this utility does not use hostkeys to verify the hosts. If this is insecure for your setup, then kindly update the code or submit a feature request.
\pysftp-0.2.9-py3.7.egg\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from \.ssh\known_hosts.  
You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
[ 2020-03-08 15:05:03,577 ] [ ][ INFO ] Attempting connection to 10.10.10.04
Traceback (most recent call last):

    File "main.py", line 183, in <module>
    downloader.run()
    File "main.py", line 107, in run
    with pysftp.Connection(host[0], username=host[1], password=host[2], cnopts=self.cnopts) as sftp:
    File "pysftp-0.2.9-py3.7.egg\pysftp\__init__.py", line 140, in __init__
    File "pysftp-0.2.9-py3.7.egg\py`enter code here`sftp\__init__.py", line 176, in _start_transport
    File "Python37-32\lib\site-packages\paramiko\transport.py", line 416, >in __init__
    "Unable to connect to {}: {}".format(hostname, reason)
    paramiko.ssh_exception.SSHException: Unable to connect to 10.10.10.04: 
    [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 log on the server:

Status: Connecting to 10.10.10.04:21...  
Status: Connection established, waiting for welcome message...  
Status: Initializing TLS...  
Status: Verifying certificate...  
Status: TLS connection established.  
Status: Logged in  
Status: Retrieving directory listing...  
Status: Directory listing of "/" successful
2

2 Answers

1
votes

You are connecting to FTP server in FileZilla.

pysftp is SFTP library.

FTP and SFTP are completely different protocols.

To connect with FTP in Python, use ftplib.

0
votes

This error can also occur if you try to connect to the wrong port.

By default, pysftp uses port 22 (the standard SFTP port), but some hosts use a different port.

Namecheap, for example, uses port 21098 for SFTP connections. To account for that, you can include a port argument in your call to pysft.Connection:

pysftp.Connection(host, username=myusername, password=mypassword, port=21098, cnopts=mycnopts)