-1

I would like to send a file through sftp with python3.6.5. I'm trying with pysftp package which seems to be a reference.

The doc says that latest version 0.2.9 (released in 2016) was checked for Python 3.4, not above (quite logic for that time). I read some comments saying it works for 3.6. I downloaded the package and try to use it, but got this mistake : AttributeError: module 'pysftp' has no attribute 'Connection'

Can't understand why .. Can someone help me out or have an other sftp package ? Thanks a lot !

Here is my code :

import pysftp
with pysftp.Connection(host=hote, username=nom, password=mdp) as sftp:
    print("Connection succesfully established ... ")
J.Delannoy
  • 345
  • 5
  • 15

2 Answers2

2

This works for me:

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None 
with pysftp.Connection(host='demo.wftpserver.com',username='demo-user',password='demo-user',port=2222,cnopts=cnopts) as sftp:
    print("Connection succesfully established ... ")


Result:  Connection succesfully established ... 

I used a public and a demo server to test the connection; and added an option to ignore checking the hostkey. My python version is 3.6.5 on Mac Mojave 10.14.6

Python 3.6.5 :: Anaconda, Inc.

BUT remove setting the hostkeys to None when you are connecting to your trusted host. Since this is important for security reason.

jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
  • Thank you for your answer ! Got this error : AttributeError: module 'pysftp' has no attribute 'CnOpts' .. I wonder if my package installation is correct – J.Delannoy Mar 09 '20 at 15:44
  • This is my pysftp==0.2.9 version. You may run this to upgrade your version. pip install --upgrade pysftp. – jose_bacoy Mar 09 '20 at 15:50
  • indeed, installation wasn't correct. The error message is now different : UserWarning: Failed to load HostKeys from C:\Users\admin.cb\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None). – J.Delannoy Mar 09 '20 at 16:04
  • great. if you are satisfied with my answer, please accept it and upvote. thanks. – jose_bacoy Mar 09 '20 at 17:06
  • Your answer improved my code but did not solved my problem. It was a problem linked to installation. Thanks for your help – J.Delannoy Mar 11 '20 at 09:05
0

As said above, pysftp works fine with python 3.6. The problem was not linked to the package pysftp itself. The package was not correctly installed. I downloaded it without pip.

So python -m pip install pysftp solved the problem.

J.Delannoy
  • 345
  • 5
  • 15