使用python的pysftp包,我在尝试上传文件时收到OSError

时间:2017-07-25 13:24:46

标签: python sftp paramiko

我使用Python 3的paramiko包来建立sftp连接并上传文件。

我可以使用以下代码成功建立与服务器的连接。

import paramiko

key_file_test = './path_to_key_file/key_file.pub'

DOWNLOAD_UAT = {
    "USERNAME": "xxxxxxxx",
    "PASSWORD": "xxxxxxxx"
}

UAT_FTP_SITE = 'sftp-test.site.com'

transport = paramiko.Transport((UAT_SFTP_SITE,22))
transport.connect(username=DOWNLOAD_UAT['USERNAME'], password=DOWNLOAD_UAT['PASSWORD'])
transport.add_server_key(key)
sftp = paramiko.SFTPClient.from_transport(transport)

print(sftp.listdir())
''' ['archiv'] '''

sftp.put('test_sftp_upload_file.txt', remotepath='./')

但是,当我运行上面的最后一行时,我得到以下错误输出。

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-69-bec34aeb9958> in <module>()
----> 1 sftp.put('test_sftp_upload_file.txt', remotepath='./')

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\sftp_client.py in put(self, localpath, remotepath, callback, confirm)
    712         file_size = os.stat(localpath).st_size
    713         with open(localpath, 'rb') as fl:
--> 714             return self.putfo(fl, remotepath, file_size, callback, confirm)
    715 
    716     def getfo(self, remotepath, fl, callback=None):

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\sftp_client.py in putfo(self, fl, remotepath, file_size, callback, confirm)
    668         .. versionadded:: 1.10
    669         """
--> 670         with self.file(remotepath, 'wb') as fr:
    671             fr.set_pipelined(True)
    672             size = self._transfer_with_callback(

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\sftp_client.py in open(self, filename, mode, bufsize)
    336             imode |= SFTP_FLAG_CREATE | SFTP_FLAG_EXCL
    337         attrblock = SFTPAttributes()
--> 338         t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
    339         if t != CMD_HANDLE:
    340             raise SFTPError('Expected handle')

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\sftp_client.py in _request(self, t, *arg)
    765     def _request(self, t, *arg):
    766         num = self._async_request(type(None), t, *arg)
--> 767         return self._read_response(num)
    768 
    769     def _async_request(self, fileobj, t, *arg):

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\sftp_client.py in _read_response(self, waitfor)
    817                 # synchronous
    818                 if t == CMD_STATUS:
--> 819                     self._convert_status(msg)
    820                 return t, msg
    821 

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\sftp_client.py in _convert_status(self, msg)
    850             raise IOError(errno.EACCES, text)
    851         else:
--> 852             raise IOError(text)
    853 
    854     def _adjust_cwd(self, path):

OSError: Bad message

我正在关注这里的文档http://docs.paramiko.org/en/2.2/api/sftp.html以构建我的代码以及paramiko包。

1 个答案:

答案 0 :(得分:1)

根据文档,您应指定完整的远程文件路径。

    :param str remotepath: the destination path on the SFTP server. Note
        that the filename should be included. Only specifying a directory
        may result in an error.