Search code examples
pythonautomationftpsftpparamiko

Unable to connect to {}: {}".format(hostname, reason)


I've my below which was working fine somedays back and suddenly now it stopped working. I get below error when I run the file.

Traceback (most recent call last):
  File "/app/www/html/project/connect.py", line 8, in <module>
    transport = paramiko.Transport((host, port))
  File "/usr/local/lib/python3.6/site-packages/paramiko/transport.py", line 416, in __init__
    "Unable to connect to {}: {}".format(hostname, reason)
paramiko.ssh_exception.SSHException: Unable to connect to mft.schneider-electric.com: [Errno 110] Connection timed out

Here is my code.

import paramiko
import os

paramiko.util.log_to_file('logfile.log')

host = "ftp.myserver.com"
port = 22
transport = paramiko.Transport((host, port))
password = "mypassword"
username = "myusername"
folder = "/Home/foldername/Automation_DKC_"

transport.connect(username=username, password=password)

sftp = paramiko.SFTPClient.from_transport(transport)

print("Successfully Connected to SFTP")

latest = 0
latestfile = None

for fileattr in sftp.listdir_attr():
    if fileattr.filename.startswith('Automation_DKC') and fileattr.st_mtime > latest:
        latest = fileattr.st_mtime
        latestfile = fileattr.filename


filepath = '/Home/foldername/' +latestfile
localpath = '/app/www/html/project/dkc.csv'
sftp.get(filepath, localpath)

sftp.close()
transport.close()

Please assist me on this. Thank you.


Solution

  • Program Works fine, it was actually Server Firewall issue and was unable to connect. My IT team resolved the issue.