用于上传和下载的 PYSFTP 脚本
PYSFTP script to Upload and Download
我需要一些帮助,我在尝试连接时收到以下消息。我正在执行的脚本如下。关于如何解决此问题的任何建议?
此外,一旦我解决了这个问题,我希望在此脚本中添加监听直到结果文件可用并从不同的远程目录下载它的能力。
提前谢谢你
No hostkey for host mysftpserver.com found.
Exception AttributeError: "'Connection' object has no attribute
'_sftp_live'" in <bound method Connection.__del__ of <pysftp.Connection
object at 0x101d8cd50>> ignored
Process finished with exit code 0
脚本
import pysftp as sftp
def sftpExample():
try:
s = sftp.Connection(host='mysftpserver', port=1211, username='myusername', password='mypassword')
remotepath='/Orders/incoming/myfile.csv'
localpath='/Users/myusername/projects/order88.csv'
s.put(localpath,remotepath)
s.close()
except Exception, e:
print str(e)
sftpExample()
根据 pysftp documentation,您需要在 ~/.ssh/known_hosts
中拥有远程主机密钥。您可以通过 ssh
命令连接到服务器来执行此操作(它会询问您是否要向主机文件添加新密钥;如果您信任远程主机的指纹,只需键入 yes
) .
官方文档还指出了如何完全禁用主机检查,但不推荐这样做,因为它会破坏 SSH 识别的关键安全功能。
pysftp documentation and other answers on Stack Overflow 似乎也暗示可以通过设置 cnopts.hostkeys = None
.
来解决错误
所以完整的代码应该是这样的:
import pysftp
def sftpExample():
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection('hostname', username='me', password='secret', cnopts=cnopts) as sftp:
sftp.put('/Users/myusername/projects/order88.csv','/Orders/incoming/myfile.csv') # upload file to public/ on remote
sftp.close()
sftpExample()
希望对您有所帮助!
我需要一些帮助,我在尝试连接时收到以下消息。我正在执行的脚本如下。关于如何解决此问题的任何建议?
此外,一旦我解决了这个问题,我希望在此脚本中添加监听直到结果文件可用并从不同的远程目录下载它的能力。
提前谢谢你
No hostkey for host mysftpserver.com found.
Exception AttributeError: "'Connection' object has no attribute
'_sftp_live'" in <bound method Connection.__del__ of <pysftp.Connection
object at 0x101d8cd50>> ignored
Process finished with exit code 0
脚本
import pysftp as sftp
def sftpExample():
try:
s = sftp.Connection(host='mysftpserver', port=1211, username='myusername', password='mypassword')
remotepath='/Orders/incoming/myfile.csv'
localpath='/Users/myusername/projects/order88.csv'
s.put(localpath,remotepath)
s.close()
except Exception, e:
print str(e)
sftpExample()
根据 pysftp documentation,您需要在 ~/.ssh/known_hosts
中拥有远程主机密钥。您可以通过 ssh
命令连接到服务器来执行此操作(它会询问您是否要向主机文件添加新密钥;如果您信任远程主机的指纹,只需键入 yes
) .
官方文档还指出了如何完全禁用主机检查,但不推荐这样做,因为它会破坏 SSH 识别的关键安全功能。
pysftp documentation and other answers on Stack Overflow 似乎也暗示可以通过设置 cnopts.hostkeys = None
.
所以完整的代码应该是这样的:
import pysftp
def sftpExample():
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection('hostname', username='me', password='secret', cnopts=cnopts) as sftp:
sftp.put('/Users/myusername/projects/order88.csv','/Orders/incoming/myfile.csv') # upload file to public/ on remote
sftp.close()
sftpExample()
希望对您有所帮助!