python paramiko: 权限被拒绝

0 投票
1 回答
5318 浏览
提问于 2025-04-16 23:18

我有一个叫做Transmit的sftp程序,用来访问一个sftp服务器。我用用户名和密码登录,一切都很顺利。我可以删除、创建文件,还能查看所有内容。

现在我需要用一个Python脚本来访问这个sftp服务器。所以我安装了parmiko这个库。我按照示例文件设置了一切,但奇怪的是,我收到了“权限被拒绝”的错误信息:

hostname = "123.456.789.1"
port = 22

hostkeytype = None
hostkey = None
try:
    host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
except IOError:
    print '*** Unable to open host keys file'
    host_keys = {}

if host_keys.has_key(hostname):
    hostkeytype = host_keys[hostname].keys()[0]
    hostkey = host_keys[hostname][hostkeytype]
    print 'Using host key of type %s' % hostkeytype

t = paramiko.Transport( (hostname, port) )
t.connect( username="customUser", password="xyzpasswd", hostkey=hostkey, pkey=None )
sftp = paramiko.SFTPClient.from_transport(t)
print sftp.listdir() # <- works
sftp.get("~/myfolder/test.png",".", None ) # <- permission denied error
t.close()

这是我运行时的输出:

Using host key of type ssh-dss
['.ssh2', 'archiv', 'myfolder']
Traceback (most recent call last):
File "/path/to/myscript.py", line 539, in <module>
main()   
File "/path/to/myscript.py", line 531, in main
ladeDatenVomSFTPServer()   
File "/path/to/myscript.py", line 493, in ladeDatenVomSFTPServer
sftp.get("~/myfolder/test.png",".", None )
File "build/bdist.macosx-10.6-intel/egg/paramiko/sftp_client.py", line 606, in get
File "build/bdist.macosx-10.6-intel/egg/paramiko/sftp_client.py", line 245, in open
File "build/bdist.macosx-10.6-intel/egg/paramiko/sftp_client.py", line 635, in _request
File "build/bdist.macosx-10.6-intel/egg/paramiko/sftp_client.py", line 682, in _read_response
File "build/bdist.macosx-10.6-intel/egg/paramiko/sftp_client.py", line 712, in _convert_status
IOError: Permission denied, file: ~/myfolder/test.png

在Transmit中一切都正常,但用parmiko就失败了。我到底哪里出错了呢?

1 个回答

0

我觉得第二个参数应该是一个文件名,而不是一个点 .

sftp.get("~/myfolder/test.png",".", None )

替换成类似下面这样的东西

sftp.get("~/myfolder/test.png","~/test.png", None )

撰写回答