Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中通过stdin发送多个字符串_Python_Python 3.x_Subprocess - Fatal编程技术网

如何在python中通过stdin发送多个字符串

如何在python中通过stdin发送多个字符串,python,python-3.x,subprocess,Python,Python 3.x,Subprocess,我通过父代码将字符串Router_Status[Router]='ON'发送到新进程 proc[Router] = subprocess.Popen([sys.executable, os.getcwd() + '/' + Router + '.py', Router, json.dumps(graph),

我通过父代码将字符串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(字节(路由器\状态[Router],编码='utf-8'))


它仍然没有做任何事情!

OK,所以我不太熟悉你传递给你的子进程的参数。PONEN(),所以我不能检查这些是不是正确的。但是我知道关于子过程模块的一两件事,还有一些事情要考虑查看你的代码:

  • 使用
    shell=False
    不需要显式定义,因为它是标准设置(尽管,如果出于某种原因希望显式定义,当然也可以)
  • 参数
    stderr=False
    不正确。它必须是
    None
    或类似于
    子流程.PIPE的内容
然后,由于您使用的是Python3.x,文档声明:

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

所以你最好使用
proc[Router].communication(input=“你的标准输入,意思是你的标准输入”)

它还指出:

Popen.stdin-如果stdin参数是管道,则此属性是 open()返回的可写流对象。如果 指定了错误参数,或者指定了universal_换行符参数 如果为True,则该流为文本流,否则为字节流。如果 stdin参数不是管道,此属性为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()语句无关。它所做的只是将用户输入写入一个文本文件,该文件只会捕获最新的输入。顺便说一句,如果要保存所有用户输入,请将
'w'
更改为
'a'
(这将使文件处于附加模式而不是写入模式)

好的,所以我不太熟悉你传递给你的子进程的参数。PONEN(),所以我不能检查这些是不是正确的。但是我知道关于子过程模块的一两件事,并且有一些事情要考虑查看你的代码:

  • 使用
    shell=False
    不需要显式定义,因为它是标准设置(尽管,如果出于某种原因希望显式定义,当然也可以)
  • 参数
    stderr=False
    不正确。它必须是
    None
    或类似于
    子流程.PIPE的内容
然后,由于您使用的是Python3.x,文档声明:

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

所以你最好使用
proc[Router].communication(input=“你的标准输入,意思是你的标准输入”)

它还指出:

Popen.stdin-如果stdin参数是管道,则此属性是 open()返回的可写流对象。如果 指定了错误参数,或者指定了universal_换行符参数 如果为True,则该流为文本流,否则为字节流。如果 stdin参数不是管道,此属性为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()语句无关。它所做的只是将用户输入写入一个文本文件,该文件只会捕获最新的输入。顺便说一句,如果要保存所有用户输入,请将
'w'
更改为
'a'
(这将使文件处于附加模式而不是写入模式).

不起作用怎么办?孩子一直被卡住?你用的是
proc[Router].wait()
在父进程中还是立即退出?我的子进程有一些无限循环子进程,直到子进程接收到新字符串='OFF'。我想发送两个stdin。在我的子进程中写入和读取它们。此外,它不会生成文件,也不会在文件中写入“开”或“关”。文件路径正确。您尝试过
pr吗oc[Router].stdin.close()
?只是想看看它在做什么。在proc[Router].stdin.write(字节(Router\u Status[Router],encoding='utf-8')+b'\n')之后,我写proc[Router].stdin.close()但是第二次我想发送'OFF'it errors'ValueError:write to closed file'
stderr=True
时,看起来也很可疑。你能设置为
stderr=subprocess.STDOUT
?怎么不起作用?孩子一直卡在那里?你用的是
proc[Router].wait()吗
在父进程中还是立即退出?我的子进程有一些无限循环子进程,直到子进程接收到新字符串='OFF'。我想发送两个stdin。在我的子进程中写入和读取它们。此外,它不会生成文件,也不会在文件中写入“开”或“关”。文件路径正确。您尝试过
pr吗oc[Router].stdin.close()
?只是想看看它在做什么。在proc[Router].stdin.write(字节(Router\u Status[Router],encoding='utf-8')+b'\n')之后,我写proc[Router].stdin.close()但是当我第二次想发送'OFF'it errors'ValueError:write to closed file'
stderr=True
时,看起来也很可疑。你能不能设置为
stderr=subprocess.STDOUT
?谢谢你的帮助。这是关于路由器的。我必须向脚本发送'ON'或'OFF'字符串,将其作为路由器的操作系统运行。例如,路由器的操作系统。'