theozh
theozh

Reputation: 25724

Cannot connect to server with Python ftplib, but PuTTY Plink works

Sorry, if this sounds too basic or if there is a duplicate which I haven't found during my search:

For example, these were not helpful:

And the examples of ftplib or from other tutorials are not working for me and the documentation is more confusing than helpful. I must be doing something elementary wrong, or maybe there are special settings with my network configuration which might prevent ftplib from working properly? My configuration. Python 3.6.3 on Win10.

My question: what's wrong with the following code?

import ftplib

host = '0.0.0.0'
user = 'user'
passwd = 'XXXX'

with ftplib.FTP(host=host, user=user, passwd=passwd) as ftp:
    print(ftp.dir())
    

After some time, I get the error message:

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


Different approach using plink:

However, the following using plink works fine for me in the command line. So, there doesn't seem to be anything wrong with the host, user or password or connection.

plink -ssh -batch [email protected] -pw XXXX "cd SubDir/SubSubDir/; ls"

But instead of messing around with plink and subprocess, I thought ftplib might be more convenient. Thank you for hints.

Upvotes: 1

Views: 1267

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202242

Plink is an SSH client. SSH has nothing to do with FTP. You probably want to connect with SFTP, what is a completely different protocol that runs on top of SSH.

For SFTP in Python, use Paramiko or pysftp.

Upvotes: 1

Ben Pinhas
Ben Pinhas

Reputation: 63

maybe try a different approach to this , first make sure u have a connection to the target IP via ping, then attempt to login with the ftplib. here is an example , let me know if it's the direction your'e going for:

import ftplib
from ftplib import FTP , error_perm
import subprocess
import time

def ping_to_target (ip):
    print(f"Ping ---> {ip}")
    process = subprocess.Popen("ping " + ip + " -n 10 " , shell=True , stdout=subprocess.PIPE)
    time.sleep(0.5)
    for line in process.stdout:
        if b"Destination" in line or b"Request timed out" in line or b"failure" in line:
            return False
    print(f"ping ---> {ip} done")
    return True

def some_func(host , username , password):
    if not ping_to_target(host):
        return False
    # CONNECT TO TARGET
    ftp = FTP (host)
    ftp.login(username , password)
    ftp.getwelcome()

i hope this is the meaning of your question

Upvotes: 0

Related Questions