Некоторые интерактивные команды выводятся по-разному, если stdin подключен к сети. Почему это?Подпроцесс Python: Почему stdin = PIPE изменяет вывод некоторых команд?
Ниже я тестирую подпроцесс. Открываем 3 разных команды, с и без stdin.
Код:
import subprocess, time
def run_command(command, enable_input):
print 'command="{}", enable_input={}:'.format(command, enable_input)
# Launch the process and set up pipes.
if enable_input:
stdin = subprocess.PIPE
else:
stdin = None
child = subprocess.Popen(command, stdin=stdin)
# Wait a second for output.
time.sleep(1)
# Terminate the child if it hasn't finished.
if child.poll() == None:
child.terminate()
print '\n-----' # Print a separator
commands = ('cmd', 'python', 'timeout 1')
for command in commands:
run_command(command, enable_input=False)
run_command(command, enable_input=True)
Выход:
command="cmd", enable_input=False:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\>
-----
command="cmd", enable_input=True:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\>
-----
command="python", enable_input=False:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
-----
command="python", enable_input=True:
-----
command="timeout 1", enable_input=False:
Waiting for 0 seconds, press a key to continue ...
-----
command="timeout 1", enable_input=True:
ERROR: Input redirection is not supported, exiting the process immediately.
-----
Ответы на вопросы, связанные ниже предполагают, что некоторые программы пытаются определить, управляются ли они человеком или сценарием. Это дело здесь? И если да, то как они обнаруживают это в Windows?
Why does supplying stdin to subprocess.Popen cause what is written to stdout to change?
Спасибо! В этом случае у меня есть другой вопрос: есть ли способ маскировки в качестве терминала (в Windows)? –
Это не отличное решение, но последовательные порты могут обрабатываться как терминалы (в зависимости от контекста), поэтому, если вы достаточно отчаянно пытаетесь использовать эмулятор нуль-модема, такой как com0com или его производное. –
Я планирую распространять свою программу, поэтому я думаю, что это добавит слишком много. Но это интересная идея. –