1

I've been looking for SFTP python packages, ftpretty works fine for me: https://pypi.org/project/ftpretty/ but I want to use a more secure protocol.

PySftp is obviously a bit outdated (Edit: it seems that pysftp is still frequently used, about the error please see below): https://bitbucket.org/dundeemt/pysftp/src/master/

And throws me several errors on Win10, PyCharm, Python3.6:

C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\bobin\.ssh\known_hosts.  You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
  warnings.warn(wmsg, UserWarning)
Traceback (most recent call last):
  File "C:/Users/bobin/PycharmProjects/classtest/pysftptest.py", line 7, in <module>
    with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py", line 132, in __init__
    self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py", line 71, in get_hostkey
    raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host s233.goserver.host found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x00000235B0695048>>
Traceback (most recent call last):
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py", line 1013, in __del__
    self.close()
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py", line 784, in close
    if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'

Process finished with exit code 1

This thread seemed relevant to me but it's already 12years old: SFTP in Python? (platform independent) And the paramiko package is also throwing me errors:

Traceback (most recent call last):
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py", line 130, in __init__
    server_version = self._send_version()
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py", line 134, in _send_version
    t, data = self._read_packet()
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py", line 201, in _read_packet
    x = self._read_all(4)
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py", line 188, in _read_all
    raise EOFError()
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/bobin/PycharmProjects/classtest/paramikotest.py", line 12, in <module>
    sftp = paramiko.SFTPClient.from_transport(transport)
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py", line 170, in from_transport
    return cls(chan)
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py", line 132, in __init__
    raise SSHException("EOF during negotiation")
paramiko.ssh_exception.SSHException: EOF during negotiation

Process finished with exit code 1

I have come so far to understand that I probably need a keyfile that I can find out by first connecting to my webspace using e.g. filezilla: How To Extract SFTP SSH Key From Key Cache in FileZilla FTP Client

My question is: how do I establish an SFTP connection to my host, which is webgo: https://www.webgo.de/hilfe/content/76/52/de/was-ist-sftp.html

EDIT: providing no host_key as follows:

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

myHostname = "host"
myUsername = "user"
myPassword = "pass"


with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts, port=22) as sftp:
    print("Connection succesfully stablished ... ")
    sftp.put('C:\TEMP\Capture.PNG', preserve_mtime=True)

still throws me an error for providing no host_keys:

C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\bobin\.ssh\known_hosts.  You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
  warnings.warn(wmsg, UserWarning)

EDIT2: tried prettyftp but my provider is refusing the connection:

Traceback (most recent call last):
  File "C:/Users/bobin/PycharmProjects/classtest/testftp.py", line 15, in <module>
    f.put('C:\TEMP\Capture.PNG', 'Capture230.PNG')
  File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\ftpretty.py", line 119, in put
    self.conn.storbinary('STOR %s' % remote_file, local_file)
  File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py", line 513, in storbinary
    return self.voidresp()
  File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py", line 249, in voidresp
    resp = self.getresp()
  File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py", line 242, in getresp
    raise error_temp(resp)
ftplib.error_temp: 425 Unable to build data connection: Operation not permitted

Used following code snippet, setting secure=False worked again:

from ftpretty import ftpretty

# Minimal
f = ftpretty('host','user','pass', port=21, secure=True)
f.put('C:\TEMP\Capture.PNG', 'Capture230.PNG')
f.close()

1 Answers1

1

For the first error, it seems like a bug in pysftp.

You can have a look at the Connection class here on line 76, and the attribute _sftp_live is defined on line 134, so this is definitely an error occurring at runtime without being validated correctly. I have also been able to find this related error, which likely explains the cause of this issue; the solution is also mentioned in the error if you want to explicitly fix it.

I would still consider using ftpretty. It does use TLS for security and a pretty safe wrapper, you can simply enable it by setting the secure parameter to True (secure=True) - which by default is set as False.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • TLS connection seems to be refused by my provider (please see my latest edit for the first post). Setting secure=False works again. – Marco Bobinger Mar 08 '21 at 07:11
  • It seems like its pretty well linked to the error [here](https://stackoverflow.com/questions/33438456/python-ftps-upload-error-425-unable-to-build-data-connection-operation-not-per). Can you follow the solution there as well as looking at the comment on the main question. – AzyCrw4282 Mar 08 '21 at 11:02