【问题标题】:Uploaded File not being recognized in SFTPSFTP 无法识别上传的文件
【发布时间】:2019-08-29 18:25:22
【问题描述】:

我正在使用 pysftp 上传远程处理的本地文件,然后返回到 SFTP,我可以在那里下载它。目前,我无法让代码识别已处理的远程文件已上传。尽管远程处理的文件已成功上传,但代码只会永远等待。

当代码运行时,如果我在 FileZilla 等服务上刷新连接,代码会立即识别出文件在其中并且运行良好。但是,我需要它在不手动刷新 FileZilla 的情况下工作。我不确定为什么代码无法识别出文件已上传并可以下载。

我已经尝试使用 while 语句断开连接然后重新连接到 SFTP,但没有成功。

import pysftp
import stat

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
srv = pysftp.Connection(host="host", username="username", password="passowrd", cnopts=cnopts)

print("Connection Made!")

# Put File In
srv.chdir(r'/path_to_remote_directory')

srv.put(r'C:\Path_to_local_file.csv')
print("File Uploaded")

while not srv.isfile(r'/Path_to_newly_uploaded_remote_file.csv'):
    time.sleep(3)
    print("Still Waiting")

if srv.isfile(r'/Path_to_newly_uploaded_remote_file.csv'):
    print("File is Ready!")
    srv.get('/Path_to_newly_uploaded_remote_file.csv', r'C:\Local_path_save_to/file.csv')

    # Closes the connection
    srv.close()

【问题讨论】:

  • /path_to_remote_directory/Path_to_newly_uploaded_remote_file.csv是什么关系?
  • 你的 SFTP 服务器是什么?告诉我们Paramiko log file
  • 如果你用listdir而不是stat来测试文件是否存在呢?

标签: python sftp pysftp


【解决方案1】:

只需删除/

srv.isfile(r'/Path_to_newly_uploaded_remote_file.csv'):
->
srv.isfile(r'Path_to_newly_uploaded_remote_file.csv'):

注意:

不要设置 cnopts.hostkeys = None, 除非你不关心安全。你失去了一个保护 这样做会造成中间人攻击。

我在我的pysftp github fork 中实现了auto_add_key

auto_add_key 将把密钥添加到known_hosts 如果auto_add_key=True
一旦known_hosts 中的主机存在密钥,就会检查该密钥。

有关安全问题,请参阅Martin Prikryl -> answer

Why using context manager is a good approach:

import pysftp
with pysftp.Connection(host, username="whatever", password="whatever", auto_add_key=True) as sftp:
    #do your stuff here
#connection closed

编辑: 检查了我的代码,/ 是问题...请检查您的文件的拼写以及您在正确的工作目录getcwd()_

import pysftp
import stat
import time

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None #do not do this !!!
srv = pysftp.Connection("host", username="user", password="pass", cnopts=cnopts)

print("Connection Made!")

# Put File In
srv.chdir(r'/upload')

srv.put(r'C:\Users\Fabian\Desktop\SFTP\testcsv.csv')
print("File Uploaded")

print(srv.getcwd()) #get the current folder ! <- you should looking here

while not srv.isfile(r'testcsv.csv'):
    time.sleep(3)
    print("Still Waiting")

if srv.isfile(r'testcsv.csv'):
    print("File is Ready!")
    srv.get('testcsv.csv', r'C:\Users\Fabian\Desktop\SFTP\testcsv_new.csv')

    # Closes the connection
    srv.close()

【讨论】:

  • 尝试按照建议删除 / ,但没有成功。即使我在 FileZilla 中刷新了 SFTP,它实际上也阻止了代码工作。为了澄清起见,当我手动刷新目录时,原始代码效果很好,只是不这样做就无法识别文件已上传。
  • @DavidSeroy 如果您的路径确实如您的问题所示,那么这个答案是有道理的。如果不是,那么您应该编辑问题以显示更多相关的路径占位符,以免混淆我们。
【解决方案2】:

找到一个可行的解决方案。我认为这是由于旧目录被缓存造成的。通过每次指向新目录进行更新,它似乎可以工作。现在明显的解决方案,但花了我一段时间。发帖以防其他人遇到这种情况。

while not srv.isfile(r'/Path_to_newly_uploaded_remote_file.csv'):
    time.sleep(3)
    srv.chdir(r'/Path_to_newly_uploaded_remote_file')
    print("Still Waiting")

【讨论】:

    猜你喜欢
    • 2016-06-18
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 2022-12-15
    • 2018-02-15
    • 1970-01-01
    相关资源
    最近更新 更多