使用pysftp进行sftp问题

时间:2017-01-11 19:05:51

标签: python sftp

我尝试使用pysftp将文件从本地计算机发送到服务器通过sftp

当我使用命令行登录服务器时:

sftp -o port=111 user_name@host

然后执行pwd我将/作为当前位置

所以我想在pysftp中做同样的事情:

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

with pysftp.Connection("host", username="user_name", password="password", port=111, cnopts=cnopts) as sftp:
    print(sftp.pwd)
    sftp.put(local_file, sftp.pwd)

但我收到错误IOError: [Errno 13] Permission denied

虽然,我能够在命令行和像FileZilla这样的ftp客户端使用相同的凭据进行sftp

这里是完整的错误跟踪:

traceback (most recent call last):
  File "C:/workspace/segments-upload/media_math_segments_upload.py", line 232, in <module>
    sys.exit(0 if main() else 1)
  File "C:/workspace/segments-upload/media_math_segments_upload.py", line 226, in main
    process(s3_bucket, s3_dir, sftp_host, sftp_path, sftp_user_name, sftp_password, sftp_port)
  File "C:/workspace/segments-upload/media_math_segments_upload.py", line 182, in process
    sftp_file(local_file, sftp_host, sftp_path, sftp_user_name, sftp_password, sftp_port)
  File "C:/workspace/segments-upload/media_math_segments_upload.py", line 105, in sftp_file
    sftp.put(local_file, sftp.pwd)
  File "C:\Users\tkaghdo\AppData\Local\Continuum\Anaconda2\lib\site-packages\pysftp\__init__.py", line 364, in put
    confirm=confirm)
  File "C:\Users\tkaghdo\AppData\Local\Continuum\Anaconda2\lib\site-packages\paramiko\sftp_client.py", line 676, in put
    return self.putfo(fl, remotepath, file_size, callback, confirm)
  File "C:\Users\tkaghdo\AppData\Local\Continuum\Anaconda2\lib\site-packages\paramiko\sftp_client.py", line 634, in putfo
    with self.file(remotepath, 'wb') as fr:
  File "C:\Users\tkaghdo\AppData\Local\Continuum\Anaconda2\lib\site-packages\paramiko\sftp_client.py", line 327, in open
    t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
  File "C:\Users\tkaghdo\AppData\Local\Continuum\Anaconda2\lib\site-packages\paramiko\sftp_client.py", line 730, in _request
    return self._read_response(num)
  File "C:\Users\tkaghdo\AppData\Local\Continuum\Anaconda2\lib\site-packages\paramiko\sftp_client.py", line 781, in _read_response
    self._convert_status(msg)
  File "C:\Users\tkaghdo\AppData\Local\Continuum\Anaconda2\lib\site-packages\paramiko\sftp_client.py", line 809, in _convert_status
    raise IOError(errno.EACCES, text)
IOError: [Errno 13] Permission denied

1 个答案:

答案 0 :(得分:0)

通过在远程服务器上提供文件名来解决该问题。所以我刚刚改变了

sftp.put(local_file, sftp.pwd)

sftp.put(local_file, "file_name.dat")