python,在pexpect中设置终端类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/358783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:57:05  来源:igfitidea点击:

python, set terminal type in pexpect

pythonpexpect

提问by Rickard Lindroth

I have a script which uses pexpect to start a CLI program. It works a bit like a shell where you get a prompt where you can enter some commands.

我有一个使用 pexpect 启动 CLI 程序的脚本。它的工作方式有点像一个 shell,在那里你会得到一个提示,你可以在其中输入一些命令。

The problem I have, I think, is that this program uses a coloured prompt.

我认为,我遇到的问题是该程序使用了彩色提示。

This is what I do

这就是我所做的

import pprint
import pexpect

1 a = pexpect.spawn('program')
2 a.expect("prompt>")
3 print "---------start------------"
4 print(a.before)
5 a.sendline("command")
6 a.expect("prompt>")
7 print "---------before------------"
8 pprint.pprint(a.before)
9 print "---------after------------"
10 pprint.pprint(a.after)

This is the output:

这是输出:

> python borken.py
---------start------------
A lot of text here from the enjoying programs start-up, lorem ipsum ...  
---------before------------
' \x1b[0m\x1b[8D\x1b[K\x1b[1m\x1b[34m'
---------after------------
'prompt>'

For some reason the first prompt colour coding borkens up things and a.before at line 8 is garbled, normal print does not work, even if I see that the command at line 5 actually produced a lot of output.

出于某种原因,第一个提示颜色编码使事情变得混乱并且 a.before 在第 8 行出现乱码,正常打印不起作用,即使我看到第 5 行的命令实际上产生了很多输出。

Does someone know what the problem could be, or is it possible to set the terminal type in pexpect to avoid the colours?

有人知道可能是什么问题,或者是否可以在 pexpect 中设置终端类型以避免颜色?

I am using tcsh shell

我正在使用 tcsh 外壳

回答by Rickard Lindroth

Ok, I found the answer. csl's answer set me on the right path.

好的,我找到了答案。csl 的回答让我走上了正确的道路。

pexpect has a "env" option which I thought I could use. like this:

pexpect 有一个我认为可以使用的“env”选项。像这样:

a = pexpect.spawn('program', env = {"TERM": "dumb"})

But this spawns a new shell which does not work for me, our development environment depends on a lot of environmental variables :/

但这会产生一个对我不起作用的新 shell,我们的开发环境取决于很多环境变量:/

But if I do this before spawning a shell:

但是,如果我在生成 shell 之前执行此操作:

import os
os.environ["TERM"] = "dumb"

I change the current "TERM" and "dumb" does not support colours, which fixed my issue.

我更改了当前的“TERM”和“dumb”不支持颜色,这解决了我的问题。

回答by csl

Couldn't find anything in the pexpect documentationfor setting terminals, but you could probably start your program explicitly with a shell, and then set the terminal type there:

pexpect 文档中找不到用于设置终端的任何内容,但您可能可以使用 shell 显式启动程序,然后在那里设置终端类型:

shell_cmd = 'ls -l | grep LOG > log_list.txt'
child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
child.expect(pexpect.EOF)

You could try something like

你可以尝试类似的东西

child = pexpect.spawn('TERM=vt100 /bin/bash', ['-c', shell_cmd])

You can also start bash with --norc and similar to avoid reading the initialization files. Check out the bash man page.

您还可以使用 --norc 和类似方法启动 bash 以避免读取初始化文件。查看bash 手册页