【问题标题】:How to send multiple string by stdin in python如何在python中通过stdin发送多个字符串
【发布时间】:2017-08-21 17:44:18
【问题描述】:

我通过父代码将字符串Router_Status[Router]='ON'发送到新进程

proc[Router] = subprocess.Popen([sys.executable, os.getcwd() + '/'
                                + Router + '.py', Router,
                                json.dumps(graph),
                                json.dumps(As_numbers_dict)],
                                shell=False, stderr=True,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)

proc[Router].stdin.write(bytes(Router_Status[Router],
                               encoding='utf-8') + b'\n')

子进程是

Router_Status[Router]=sys.stdin.readline().strip()
path = os.path.expanduser('~' + '/BGP_Routers/' + Router)
with open(path + '/Router_Status.txt', 'w') as f:
        f.write(Router_Status[Router])

但它不起作用! 然后我将第二个字符串 Router_Status[Router]='OFF' 通过 proc[Router].stdin.write(bytes(Router_Status[Router], encoding='utf-8')

proc[Router].stdin.flush()

它仍然没有做任何事情!

【问题讨论】:

  • 不起作用怎么办?孩子卡住了?您是在父进程中使用proc[Router].wait() 还是立即退出?
  • 我的子进程有一些无限循环子进程,直到子进程收到新的字符串='OFF'。我想发送两个标准输入。在我的子进程中编写和阅读它们。它也不会生成文件,也不会在文件中写入“ON”或“OFF”。文件路径正确。
  • 你试过proc[Router].stdin.close() 吗?只是看看它在做什么
  • 是在 proc[Router].stdin.write(bytes(Router_Status[Router],encoding='utf-8') + b'\n') 之后我写了 proc[Router].stdin。 close() 但第二次当我想发送'OFF'时出错'ValueError: write to closed file'
  • stderr=True 看起来也很可疑。你能设置成stderr=subprocess.STDOUT吗?

标签: python python-3.x subprocess


【解决方案1】:

好的,所以我并不完全熟悉您传递给 subprocess.Popen() 的参数,因此我无法检查这些参数是否正确。但是,我确实对 subprocess 模块了解一两件事,并且有一些事情需要考虑查看您的代码:

  • 使用shell=False 不需要明确定义,因为它是标准设置(尽管,如果您想明确说明,那当然可以)。
  • 参数stderr=False 不正确。它必须是 None 或类似 subprocess.PIPE 的东西。

然后,由于您使用的是 Python3.x,因此文档声明:

警告 - 使用communicate() 而不是.stdin.write、.stdout.read 或 .stderr.read 以避免由于任何其他操作系统管道导致的死锁 缓冲区填满并阻塞子进程。

所以你最好使用proc[Router].communicate(input="your standard input, meaning your stdin")

它还指出:

Popen.stdin - 如果标准输入参数是 PIPE,则此属性是 open() 返回的可写流对象。如果编码或 指定了错误参数或universal_newlines参数是 是的,流是文本流,否则是字节流。如果 stdin 参数不是 PIPE,此属性为 None。

在您的情况下,stdin 确实应该通过字节流来表示。

总的来说,我猜你应该这样做:

proc[Router] = subprocess.Popen([sys.executable, os.getcwd() + '/'
                                + Router + '.py', Router,
                                json.dumps(graph),
                                json.dumps(As_numbers_dict)],
                                stderr=subprocess.PIPE,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)

proc[Router].stdin.communicate(str(Router_Status[Router]).encode())

但我仍然想知道,您为什么要包括:

path = os.path.expanduser('~' + '/BGP_Routers/' + Router)
with open(path + '/Router_Status.txt', 'w') as f:
        f.write(Router_Status[Router])

因为它基本上与 subprocess.Popen() 语句无关。它所做的只是将用户输入写入文本文件,该文件只会捕获最新的输入 btw。如果您想保存所有用户输入,请将'w' 更改为'a'(这会将文件置于附加模式而不是写入模式)。

【讨论】:

  • 感谢您的帮助。这是关于路由器的。我必须将 'ON' 或 'OFF' 字符串发送到脚本以将其作为路由器的 Os 运行。例如 Router_statues='ON'。然后在 [sys.executable, os.getcwd() + '/' + Router + '.py' 我只是调用脚本并将'ON'传递给它。我的问题是如何在脚本中读取“ON”字符串???
  • 意思是你想调用命令行:[sys.executable, os.getcwd() + '/' + Router + '.py'] 并简单地将'ON' 作为参数传递?如果是这样,你为什么不试试[sys.executable, os.getcwd() + '/' + Router + '.py', 'ON'],它会这样做......或者你的意思是你想打开~/Router.py并阅读它的内容,看看它是否设置为'ON'或@987654336 @?
  • 在您的问题中添加一些pseudo-code 可能有助于澄清您的意思..否则它仍然有点模糊..
  • 我有 10 个脚本,每个脚本都是一个路由器。 [Sys.executable] 命令执行 [os.getcwd() + '/' + Router + '.py'] 路径中的脚本。每个脚本都是路由器的操作系统。然后我必须传递参数。
猜你喜欢
  • 1970-01-01
  • 2014-02-09
  • 2020-08-16
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
相关资源
最近更新 更多