paramiko.sftp.SFTPError:预期的属性

问题描述

运行Python代码时出现以下错误

sftp.put(local_path,remote_path,callback=track_progress,confirm=True)

但是,如果我输入confirm=False,则不会出现此错误track_progress的定义如下:

def track_progress(bytes_transferred,bytes_total):
    total_percent = 100
    transferred_percent = (bytes_transferred * total_percent) / bytes_total
    result_str = f"Filename: {file},File Size={str(bytes_total)}b |-->
                 " f" Transfer Details ={str(transferred_percent)}% " \ f"({str(bytes_transferred)}b)Transferred"
    #self.logger.info(result_str)
    print(result_str)

任何人都可以在这里帮助我理解这个问题。

Traceback (most recent call last):
  File "D:/Users/prpandey/PycharmProjects/PMPPractise/Transport.py",line 59,in <module>
    sftp.put(local_path,confirm=True)
  File "D:\Users\prpandey\PycharmProjects\PMPPractise\venv\lib\site-packages\paramiko\sftp_client.py",line 759,in put
    return self.putfo(fl,remotepath,file_size,callback,confirm)
  File "D:\Users\prpandey\PycharmProjects\PMPPractise\venv\lib\site-packages\paramiko\sftp_client.py",line 720,in putfo
    s = self.stat(remotepath)
  File "D:\Users\prpandey\PycharmProjects\PMPPractise\venv\lib\site-packages\paramiko\sftp_client.py",line 495,in stat
    raise SFTPError("Expected attributes")
paramiko.sftp.SFTPError: Expected attributes

Paramiko日志文件

enter image description here


根据建议,我已经尝试过:

sftp.put(local_path,confirm=False)
t,msg = sftp._request(CMD_STAT,remote_path)

t是101。

解决方法

设置confirm=True时,SFTPClient.put向服务器询问刚上传的文件的大小。这样做是为了验证文件大小是否与源本地文件的大小匹配。另请参见Paramiko put method throws "[Errno 2] File not found" if SFTP server has trigger to automatically move file upon upload

大小请求使用SFTP“ STAT”消息,服务器应向其返回“ ATTRS”消息(文件属性)或带有错误的“ STATUS”消息(101)。您的服务器似乎返回状态为“ OK”的“ STATUS”消息(我的猜测基于您和Paramiko源代码的数据)。 “ OK”是对“ STAT”请求的无效响应。 Paramiko不会期望这样的废话响应,因此它报告了一些不清楚的错误。但这最终是服务器的错误。您所要做的就是通过设置confirm=False来禁用验证。