0

I'm trying to connect through Paramiko to an external SFTP and passing an encrypted config file for the credentials. When attempting to connect with the following, I get an error for the exception client.close that states AttributeError: 'Nonetype' object has not attribute 'close':

client = None
try:
    client = paramiko.Transport(hostname, port)
    client.connect(username=username, password=password)
except Exception as e:
    client.close()
    return 'Cannot connect to SFTP server: ' + str(e.args[-1]), []
# Go
sftp = paramiko.SFTPClient.from_transport(client)
  • is hostname/port known ? it definitely fails to create the `client` – hootnot Jul 09 '18 at 18:17
  • see, https://stackoverflow.com/questions/3635131/paramikos-sshclient-with-sftp, it seems that host, port need to be passed as a tuple: `paramiko.Transport((hostname, port))` – hootnot Jul 09 '18 at 18:21

1 Answers1

0

In the exception you're calling method client.close() which throws an error because the method client.connect did not succeeded, remove this method or create an exception for each possible error with to code handling that specific error.

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
  • This code works for another customer and I'm able to use the credentials through other means to login (winscp and mirth), but this fails from the get go. Is there a retry that can be attempted or another way to connect? – JKnuevil Jul 09 '18 at 18:19
  • I will focus on the main question you asked: the exception code is wrong because it calls method `close` which must be called only when `connect` succeeded. – Baptiste Mille-Mathias Jul 09 '18 at 18:22