简体   繁体   English

subprocess.Popen()读取文件

[英]subprocess.Popen() read a file

I'm trying to use subprocess to read a file stored in a remote server. 我正在尝试使用子进程读取存储在远程服务器中的文件。

import subprocess
import sys

ssh = subprocess.Popen(['ssh', 'hjh:passwd@myserver', 'cat', 'data/test.txt'],
                   shell=False,
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
    error = ssh.stderr.readlines()
    print >>sys.stderr, "ERROR: %s" % error
else:
    print result

Now running this results in the error 现在运行此结果将导致错误

Traceback (most recent call last):
  File "C:/Users/hjh/Desktop/try.py", line 15, in <module>
stderr=subprocess.PIPE)
  File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Does this error mean that the file on the server cannot be found? 此错误是否表示找不到服务器上的文件? or is there an error in subprocess? 还是子过程中有错误? Google wasn't much of a help for me on this error. Google对这个错误的帮助并不大。

I also work with a Proxy which is not specified here, can that be a problem? 我也使用未在此处指定的代理服务器,这可能是个问题吗?

Cheers, 干杯,

I suppose that Windows is unable to find the SSH executable. 我想Windows无法找到SSH可执行文件。 Check what happens when you type ssh in the cmd.exe . 检查在cmd.exe键入ssh时会发生什么。 Try adding a full path to the SSH client executable in the Popen call or add SSH's directory to the PATH environment variable. 尝试在Popen调用中为SSH客户端可执行文件添加完整路径,或将SSH的目录添加到PATH环境变量中。

It seems like "ssh" cannot be resolved. 似乎无法解决“ ssh”。 I would either fully qualify the location to the ssh executable in the Popen constructor call and/or it is in the searchable PATH. 我可以完全限定Popen构造函数调用中ssh可执行文件的位置和/或它位于可搜索的PATH中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM