我目前正在尝试自动化简单的颜色读取测试。我使用 nc 连接到该服务,它给我一个特定颜色的文本,然后提示用户命名上面的彩色文本。
这看起来像这样: 数控 这是你的颜色:颜色! (例如绿色)
>绿色
好的!
这是你的颜色:颜色! (例如蓝色)
>红色
错误!不行!
正在退出...
我当前的代码看起来像这样,但我不知道如何实现它。
#!/usr/bin/python
import sys
import socket
from pwn import *
hostname='ip'
port=777
r = remote(hostname, port)
def recv(string):
print r.recv()
print r.recvuntil(string)
def send(string):
print r.send(string)
#Loop through orders and check for order
while True:
recv("Here is your color:")
color=r.recvuntil("color!") #Receive color
print "Color is:" + command
recv(">") #Receive until input prompt
if color=green: #Check which color
send("green")
else:
send("else")
该解决方案应该使其无限运行,而无需用户提示任何内容。
最佳答案
现在已经解决了。 我通过打印接收到的字符串的原始版本找到了颜色。
repr(color)
对于绿色文本,结果是:
\x1b[92mGreen
然后我只是检查颜色标识符 (\x1b[92m) 是否在字符串中。
关于python - 获取终端输出的颜色/返回文本的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56431673/