【问题标题】:Get color of terminal output / color of text returned获取终端输出颜色/返回文本颜色
【发布时间】:2019-10-19 06:19:00
【问题描述】:

我目前正在尝试自动化一个简单的颜色阅读测试。我用 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")

该解决方案应使其无休止地运行,而无需用户提示任何内容。

【问题讨论】:

  • 您是否检查过收到的字符串以了解它们使用哪些控制代码来控制颜色?

标签: python sockets colors terminal pwntools


【解决方案1】:

现在解决了。 我通过打印接收到的字符串的原始版本找到了颜色。

repr(color)

对于绿色文本,结果如​​下:

\x1b[92m绿色

然后我简单地检查了颜色标识符 (\x1b[92m) 是否在字符串中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 2015-02-27
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多