簡體   English   中英

從程序獲取帶有用戶輸入的程序輸出

[英]Get output from a program with the user input that program takes

我正在嘗試從子流程的輸出中捕獲字符串,並且當子流程要求用戶輸入時,請將用戶輸入包括在字符串中,但是我無法使stdout正常工作。

我使用while循環從stdout獲得了字符串輸出,但是在讀取字符串之后我不知道如何終止它。

我嘗試使用subprocess.check_output ,但隨后看不到用戶輸入的提示。

import subprocess
import sys
child = subprocess.Popen(["java","findTheAverage"], stdout = subprocess.PIPE, stdin = subprocess.PIPE )
string = u""

while True:
    line = str(child.stdout.read(1))
    if line != '':
        string += line[2]
        print(string)
    else:
        break

print(string)
for line in sys.stdin:
    print(line)
    child.stdin.write(bytes(line, 'utf-8'))

編輯:

有了Alfe post的幫助和代碼,我現在有了從子流程程序輸出中創建的字符串,並且用戶對該程序進行了輸入,但是混亂了。

該字符串似乎首先獲取輸出的第一個字母,然后是用戶輸入,然后是輸出的其余部分。

字符串混淆的示例:

U2
3ser! please enter a double:U
4ser! please enter another double: U
5ser! please enter one final double: Your numbers were:
a = 2.0
b = 3.0
c = 4.0
average = 3.0

的意思是:

User! please enter a double:2
User! please enter another double: 3
User! please enter one final double: 4
Your numbers were:
a = 2.0
b = 3.0
c = 4.0
average = 3.0

使用代碼:

import subprocess
import sys
import signal
import select


def signal_handler(signum, frame):
    raise Exception("Timed out!")


child = subprocess.Popen(["java","findTheAverage"], universal_newlines = True, stdout = subprocess.PIPE, stdin = subprocess.PIPE )
string = u""
stringbuf = ""

while True:
  print(child.poll())
  if child.poll() != None and not stringbuf:
    break
  signal.signal(signal.SIGALRM, signal_handler)
  signal.alarm(1)
  try:
    r, w, e = select.select([ child.stdout, sys.stdin ], [], [])
    if child.stdout in r:
        stringbuf = child.stdout.read(1)
        string += stringbuf
        print(stringbuf)
  except:
    print(string)
    print(stringbuf)
  if sys.stdin in r:
    typed = sys.stdin.read(1)
    child.stdin.write(typed)
    string += typed

最終編輯:

好吧,我試用了它,並使其與以下代碼一起使用:

import subprocess
import sys
import select
import fcntl
import os

# the string that we will return filled with tasty program output and user input #
string = ""

# the subprocess running the program #
child = subprocess.Popen(["java","findTheAverage"],bufsize = 0, universal_newlines = True, stdout = subprocess.PIPE, stdin = subprocess.PIPE )

# stuff to stop IO blocks in child.stdout and sys.stdin ## (I stole if from  http://stackoverflow.com/a/8980466/2674170)
fcntl.fcntl(child.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

# this here in the unlikely event that the program has #
# finished by the time the main loop is first running  #
# because if that happened the loop would end without  #
# having added the programs output to the string!      #
progout = ""
typedbuf = "#"

### here we have the main loop, this friendly fellah is 
### going to read from the program and user, and tell
### each other what needs to be known
while True:

## stop when the program finishes and there is no more output
  if child.poll() != None and not progout:
    break

# read from 
  typed = ""

  while typedbuf:
    try:
      typedbuf = sys.stdin.read(1)
    except:
      break
    typed += typedbuf
    stringbuf  = "#"
  string += typed
  child.stdin.write(typed)

  progout = ""
  progoutbuf = "#"
  while progoutbuf:
    try:
      progoutbuf = child.stdout.read(1)
    except:
      typedbuf = "#"
      break 
    progout += progoutbuf
  if progout:
    print(progout)
  string += progout

# the final output string #
print( string)

您需要select同時讀取多個源(在您的情況下為stdin和子進程的輸出)。

import select

string = ''
while True:
  r, w, e = select.select([ child.stdout, sys.stdin ], [], [])
  if child.stdout in r:
    string += child.stdout.read()
  if sys.stdin in r:
    typed = sys.stdin.read()
    child.stdin.write(typed)
    string += typed

您仍然需要找到合適的中斷條件才能退出該循環。 但是您可能已經知道了。

在這一點上,我想發出警告:寫入管道的進程通常會緩沖到最新的時刻; 您可能不會想到這一點,因為從命令行(在終端中)測試同一程序時,通常僅緩沖行。 這是出於性能方面的考慮。 寫入終端時,通常用戶希望盡快看到輸出。 在寫入管道時,通常會為讀取過程提供較大的塊,以便在它們到達之前休眠更長時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM