【问题标题】:file existence checker on a remote serveralways returning 255远程服务器上的文件存在检查器总是返回 255
【发布时间】:2021-06-11 15:20:56
【问题描述】:

当我尝试运行此代码时(变量名已更改)。由于某种原因,它总是返回 255。我正在尝试检查远程服务器上是否存在具有特定名称的文件。如果没有,这里是否有任何错误,那么我可以使用任何其他方法来实现检查远程服务器中是否存在具有特定名称的文件的目的。 (Python paramiko 库是我用来连接服务器的。)

import subprocess
import pipes

ssh_host = 'host_adress'
file = 'file_loc'

resp = subprocess.call(
    ['ssh', ssh_host, 'test -e ' + pipes.quote(file)])

if resp == 0:
    print ('%s exists' % file)
else:
    print ('%s does not exist' % file)

【问题讨论】:

  • 抱歉回复晚了,我终于弄明白了。我放弃了这个解决方案,只使用了一个 filenotfound 错误和一个 try/except 函数。

标签: python subprocess paramiko remote-server


【解决方案1】:

使用 requests 包似乎有效:

import requests

source_url = "https://blah blah"

try:
    downloaded_data = requests.get(source_url)
    print("Success")
except:
    print("Data cannot be downloaded")

【讨论】:

  • 我正在尝试查看服务器上是否存在文件而不是下载文件。如果我已经使用 paramiko 连接到服务器,请求数据包会这样做吗?
猜你喜欢
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 2014-01-31
  • 2014-12-07
  • 2013-01-01
  • 2010-12-12
  • 1970-01-01
  • 2011-06-03
相关资源
最近更新 更多