Diego
Diego

Reputation: 167

Python with SFTP library fails with correct code

Here is my code:

import pysftp

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

host = "192.168.8.104"
username = "pi"
password = "raspberry"

local_path = "C:\Users\helencecilia\Desktop\Basura\Conexion SFTP\download.txt" 
remote_path = "\home\pi\helloworld.txt"

with pysftp.Connection(host, username, password, cnopts=cnopts) as sftp:
    sftp.put(local_path, remote_path)

Error message:

C:\Python27\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\helencecilia\.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\helencecilia\Desktop\Basura\Conexion SFTP\prueba.py", line 12, in <module>
    with pysftp.Connection(host, username, password, cnopts=cnopts) as sftp:
  File "C:\Python27\lib\site-packages\pysftp\__init__.py", line 142, in __init__
    self._set_authentication(password, private_key, private_key_pass)
  File "C:\Python27\lib\site-packages\pysftp\__init__.py", line 167, in _set_authentication
    private_key_file, private_key_pass)
  File "C:\Python27\lib\site-packages\paramiko\pkey.py", line 196, in from_private_key_file
    key = cls(filename=filename, password=password)
  File "C:\Python27\lib\site-packages\paramiko\rsakey.py", line 45, in __init__
    self._from_private_key_file(filename, password)
  File "C:\Python27\lib\site-packages\paramiko\rsakey.py", line 163, in _from_private_key_file
    data = self._read_private_key_file('RSA', filename, password)
  File "C:\Python27\lib\site-packages\paramiko\pkey.py", line 267, in _read_private_key_file
    with open(filename, 'r') as f:
IOError: [Errno 2] No such file or directory: 'raspberry'

Upvotes: 1

Views: 198

Answers (1)

Selcuk
Selcuk

Reputation: 59184

The third argument must be private_key, not password according to the API documentation:

class pysftp.Connection(host, username=None, private_key=None, password=None, port=22, private_key_pass=None, ciphers=None, log=False, cnopts=None, default_path=None)

Either pass None for the private_key argument:

pysftp.Connection(host, username, None, password, cnopts=cnopts)

or better, use keyword arguments:

pysftp.Connection(host=host, username=username, password=password, cnopts=cnopts)

Upvotes: 1

Related Questions