Linux:将管道输入到Python(ncurses)脚本,stdin和termios
看起来这个问题几乎和“在Python中从stdin读取时出现坏管道文件描述符 - Stack Overflow”重复了,不过我觉得这个情况稍微复杂一点(而且并不是特定于Windows的,因为那个讨论的结论是这样)。
我现在正在尝试用Python写一个简单的脚本:我想给这个脚本提供输入,可以通过命令行参数来提供;或者通过“管道”将一个字符串传递给这个脚本,然后让脚本用curses
终端界面显示这个输入字符串。
完整的脚本,这里叫做testcurses.py
,如下所示。问题是每当我尝试实际的管道操作时,似乎会搞乱stdin,curses
窗口根本不显示。这里是终端输出:
## CASE 1: THROUGH COMMAND LINE ARGUMENT (arg being stdin):
##
$ ./testcurses.py -
['-'] 1
stdout/stdin (obj): <open file '<stdout>', mode 'w' at 0xb77dc078> <open file '<stdin>', mode 'r' at 0xb77dc020>
stdout/stdin (fn): 1 0
env(TERM): xterm xterm
stdin_termios_attr [27906, 5, 1215, 35387, 15, 15, ['\x03', ... '\x00']]
stdout_termios_attr [27906, 5, 1215, 35387, 15, 15, ['\x03', ... '\x00']]
opening -
obj <open file '<stdin>', mode 'r' at 0xb77dc020>
TYPING blabla HERE
wr TYPING blabla HERE
at end
before curses TYPING blabla HERE
#
# AT THIS POINT:
# in this case, curses window is shown, with the text 'TYPING blabla HERE'
# ################
## CASE 2: THROUGH PIPE
##
## NOTE I get the same output, even if I try syntax as in SO1057638, like:
## python -c "print 'TYPING blabla HERE'" | python testcurses.py -
##
$ echo "TYPING blabla HERE" | ./testcurses.py -
['-'] 1
stdout/stdin (obj): <open file '<stdout>', mode 'w' at 0xb774a078> <open file '<stdin>', mode 'r' at 0xb774a020>
stdout/stdin (fn): 1 0
env(TERM): xterm xterm
stdin_termios_attr <class 'termios.error'>::(22, 'Invalid argument')
stdout_termios_attr [27906, 5, 1215, 35387, 15, 15, ['\x03', '\x1c', '\x7f', '\x15', '\x04', '\x00', '\x01', '\xff', '\x11', '\x13', '\x1a', '\xff', '\x12', '\x0f', '\x17', '\x16', '\xff', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00']]
opening -
obj <open file '<stdin>', mode 'r' at 0xb774a020>
wr TYPING blabla HERE
at end
before curses TYPING blabla HERE
#
# AT THIS POINT:
# script simply exits, nothing is shown
# ################
据我所见,问题在于:每当我们将字符串通过管道传入Python脚本时,Python脚本就失去了对终端的stdin
引用,并且发现替换后的stdin
不再是termios
结构了——而且因为stdin
不再是终端,所以curses.initscr()
会立即退出,什么都不渲染。
所以,我的问题简而言之就是:我能否以某种方式实现,让语法echo "blabla" | ./testcurses.py -
最终在curses
中显示管道传入的字符串?更具体地说:是否可以从Python脚本中获取调用终端的stdin
的引用,即使这个脚本是被“管道”传递的?
提前感谢任何指点,
谢谢!
附注:testcurses.py
脚本:
#!/usr/bin/env python
# http://www.tuxradar.com/content/code-project-build-ncurses-ui-python
# http://diveintopython.net/scripts_and_streams/stdin_stdout_stderr.html
# http://bytes.com/topic/python/answers/42283-curses-disable-readline-replace-stdin
#
# NOTE: press 'q' to exit curses - Ctrl-C will screw up yer terminal
# ./testcurses.py "blabla" # works fine (curseswin shows)
# ./testcurses.py - # works fine, (type, enter, curseswins shows):
# echo "blabla" | ./testcurses.py "sdsd" # fails to raise curses window
#
# NOTE: when without pipe: termios.tcgetattr(sys.__stdin__.fileno()): [27906, 5, 1215, 35387, 15, 15, ['\x03',
# NOTE: when with pipe | : termios.tcgetattr(sys.__stdin__.fileno()): termios.error: (22, 'Invalid argument')
import curses
import sys
import os
import atexit
import termios
def openAnything(source):
"""URI, filename, or string --> stream
http://diveintopython.net/xml_processing/index.html#kgp.divein
This function lets you define parsers that take any input source
(URL, pathname to local or network file, or actual data as a string)
and deal with it in a uniform manner. Returned object is guaranteed
to have all the basic stdio read methods (read, readline, readlines).
Just .close() the object when you're done with it.
"""
if hasattr(source, "read"):
return source
if source == '-':
import sys
return sys.stdin
# try to open with urllib (if source is http, ftp, or file URL)
import urllib
try:
return urllib.urlopen(source)
except (IOError, OSError):
pass
# try to open with native open function (if source is pathname)
try:
return open(source)
except (IOError, OSError):
pass
# treat source as string
import StringIO
return StringIO.StringIO(str(source))
def main(argv):
print argv, len(argv)
print "stdout/stdin (obj):", sys.__stdout__, sys.__stdin__
print "stdout/stdin (fn):", sys.__stdout__.fileno(), sys.__stdin__.fileno()
print "env(TERM):", os.environ.get('TERM'), os.environ.get("TERM", "unknown")
stdin_term_attr = 0
stdout_term_attr = 0
try:
stdin_term_attr = termios.tcgetattr(sys.__stdin__.fileno())
except:
stdin_term_attr = "%s::%s" % (sys.exc_info()[0], sys.exc_info()[1])
try:
stdout_term_attr = termios.tcgetattr(sys.__stdout__.fileno())
except:
stdout_term_attr = `sys.exc_info()[0]` + "::" + `sys.exc_info()[1]`
print "stdin_termios_attr", stdin_term_attr
print "stdout_termios_attr", stdout_term_attr
fname = ""
if len(argv):
fname = argv[0]
writetxt = "Python curses in action!"
if fname != "":
print "opening", fname
fobj = openAnything(fname)
print "obj", fobj
writetxt = fobj.readline(100) # max 100 chars read
print "wr", writetxt
fobj.close()
print "at end"
sys.stderr.write("before ")
print "curses", writetxt
try:
myscreen = curses.initscr()
#~ atexit.register(curses.endwin)
except:
print "Unexpected error:", sys.exc_info()[0]
sys.stderr.write("after initscr") # this won't show, even if curseswin runs fine
myscreen.border(0)
myscreen.addstr(12, 25, writetxt)
myscreen.refresh()
myscreen.getch()
#~ curses.endwin()
atexit.register(curses.endwin)
sys.stderr.write("after end") # this won't show, even if curseswin runs fine
# run the main function - with arguments passed to script:
if __name__ == "__main__":
main(sys.argv[1:])
sys.stderr.write("after main1") # these won't show either,
sys.stderr.write("after main2") # (.. even if curseswin runs fine ..)
2 个回答
问题是,每当我尝试实际的管道操作时,这似乎会搞乱标准输入(stdin),而且 curses 窗口从来没有显示出来。 [...省略...] 据我所知,问题是:每当我们将字符串通过管道传入 Python 脚本时,Python 脚本就失去了对终端作为标准输入的引用,并且发现替换后的标准输入不再是一个 termios 结构了。由于标准输入不再是终端,curses.initscr() 会立即退出,而什么都不渲染。
其实,curses 窗口是会显示的,但因为在你新的标准输入上没有更多的输入,myscreen.getch()
会立即返回。所以这和 curses 检查标准输入是否是终端没有关系。
所以如果你想使用 myscreen.getch()
和其他 curses 输入函数,你需要重新打开你的终端。在 Linux 和其他类 Unix 系统中,通常有一个叫 /dev/tty
的设备,它指的是当前的终端。所以你可以在调用 myscreen.getch()
之前做一些类似这样的操作:
f=open("/dev/tty")
os.dup2(f.fileno(), 0)
在你调用 myscreen.getch()
之前。
这个事情不能不让父进程参与进来。不过幸运的是,有一种方法可以让 bash 参与进来,使用 输入输出重定向:
$ (echo "foo" | ./pipe.py) 3<&0
这样就可以把 foo
传给 pipe.py
,并在一个子进程中把 stdin
复制到文件描述符 3。现在我们只需要在 Python 脚本中利用父进程的这个额外帮助(因为我们会继承 fd 3):
#!/usr/bin/env python
import sys, os
import curses
output = sys.stdin.readline(100)
# We're finished with stdin. Duplicate inherited fd 3,
# which contains a duplicate of the parent process' stdin,
# into our stdin, at the OS level (assigning os.fdopen(3)
# to sys.stdin or sys.__stdin__ does not work).
os.dup2(3, 0)
# Now curses can initialize.
screen = curses.initscr()
screen.border(0)
screen.addstr(12, 25, output)
screen.refresh()
screen.getch()
curses.endwin()
最后,你可以通过先运行子进程来避免命令行上那些复杂的语法:
$ exec 3<&0 # spawn subshell
$ echo "foo" | ./pipe.py # works
$ echo "bar" | ./pipe.py # still works
这样就解决了你的问题,前提是你有 bash
。