使用PAramiko'放入'SFTP

时间:2010-06-22 08:09:35

标签: python sftp paramiko

我已经安装并编写了以下无法放入文件的Paramiko。它很容易“获取”文件并在其上执行'ls'命令。

#set username & password
username='runaway'
password='runaway'
port=22
source= '/Unzip.sh' 
destination ='/var/mpx/www/http'


#SFTP
client.load_system_host_keys()
print " hostname =%s \n username=%s \n password=%s \n" (hostname,username,password)
t = paramiko.Transport((hostname, port)) 
t.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(source,destination)
#sftp.close()
#t.close()

使用'put'命令会出现以下错误&堆栈跟踪 -

File "upload_file.py", line 84, in ?
    sftp.put(source,destination)
  File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 522, in put
    fr = self.file(remotepath, 'wb')
  File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 221, in open
    t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
  File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 572, in _request
    return self._read_response(num)
  File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 619, in _read_response
    self._convert_status(msg)
  File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 649, in _convert_status
    raise IOError(text)
IOError: Failure

我如何克服这个问题?

2 个答案:

答案 0 :(得分:63)

解决方案对我来说似乎很有趣!

source= '/Unzip.sh' 
destination ='/var/mpx/www/http/Unzip.sh'

刚刚修改了目标路径以包含文件名。 没想到会有这样的错误来自Python包。

答案 1 :(得分:0)

当你尝试sftp.mkdir('/exists')

时,这也发生在2.0.2中
Traceback (most recent call last):
  ...
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 380, in mkdir
    self._request(CMD_MKDIR, path, attr)
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 730, in _request
    return self._read_response(num)
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 781, in _read_response
    self._convert_status(msg)
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 811, in _convert_status
    raise IOError(text)
IOError: Failure

这是我的Python 2.7.9修复:

try:
    sftp.mkdir(remote_dir)
except IOError:
    logging.debug('%s already exists.', remote_dir)