“放”在 SFTP 中使用 Paramiko

发布于 2024-09-06 09:40:44 字数 1341 浏览 5 评论 0原文

我已经安装并编写了以下无法放置文件的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

我该如何克服这个问题?

I've installed and written the following Paramiko which is unable to put the file. It is easily able to 'get' a file and execute ls commands on it.

#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()

Using a 'put' command gives the following error & stack trace -

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

How do I overcome this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2024-09-13 09:40:44

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

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

只是修改了目标路径以包含文件名。
没想到 Python 包会出现这样的错误。

The solution seemed very funny to me!

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

Just modified the destination path to include the file name as well.
Didn't expect some error like this coming from a Python package.

冷夜 2024-09-13 09:40:44

当您尝试 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)

This also occurs in 2.0.2 when you try to sftp.mkdir('/exists'):

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

This was my Python 2.7.9 fix:

try:
    sftp.mkdir(remote_dir)
except IOError:
    logging.debug('%s already exists.', remote_dir)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文