【问题标题】:How to pick out the second to last line from a telnet command如何从 telnet 命令中选择倒数第二行
【发布时间】:2019-12-04 06:45:10
【问题描述】:

像我打开的许多其他线程一样,我正在尝试使用通过 Telnet 运行的 blackmagic hyperdeck 创建一个多功能即时回放系统。我正在尝试实现的当前功能是一个输入输出回放,它需要以 hh:mm:ss;ff 格式存储两个时间码变量,其中 h=hours,m=minutes,s=seconds,f=frames @30fps .用于此的 telnet 命令是传输信息,响应返回 9 行,我只想要第 7 行的时间码。关于如何做到这一点的任何想法,因为它超出了我的范围?

status: stopped
speed: 0
slot id: 1
clip id: 1
single clip: false
display timecode: 00:00:09;22
timecode: 00:00:09;22
video format: 1080i5994
loop: false

这是我想要的理想外观

import telnetlib

host = "192.168.1.13" #changes for each device
port = 9993 #specific for hyperdecks
timeout = 10

session = telnetlib.Telnet(host, port, timeout)

def In():
    session.write(b"transport info \n")
    line = session.read_until(b";00",.5)
    print(line)
    #code to take response and store given line as variable IOin
def out():
    session.write(b"transport info \n")
    line = session.read_until(b";00",.5)
    print(line)
    #code to take response and store given line as variable IOout
def IOplay():
    IOtc = "playrange set: in: " + str(IOin) + " out: " + str(IOout) + " \n"
    session.write( IOtc.encode() )
    speed = "play: speed: " + str(Pspeed.get() ) + "\n"
    session.write(speed.encode() )

【问题讨论】:

    标签: python-3.x telnetlib


    【解决方案1】:
    def get_timecode(text):
        tc = ''
        lines = text.split('\r\n')
        for line in lines:
            var, val = line.split(': ', maxsplit=1)
            if var == 'timecode':
                tc = val
        return tc
    

    您可以选择直接访问lines[6],无需扫描, 但是如果客户端与服务器不同步,那将更加脆弱, 或者如果服务器的输出格式在以后的版本中发生了变化。

    编辑:

    你写道:

    session.write(b"transport info \n")
    

    #code 获取响应并将给定行存储为变量 IOin

    您似乎不是会话中的任何reading。 我不使用 telnetlib,但文档建议您使用 如果您不执行以下操作,则永远不会获得这九行文本:

    expect = b"foo"  # some prompt string returned by server that you never described in your question
    session.write(b"transport info\n")
    bytes = session.read_until(expect, timeout)
    text = bytes.decode()
    print(text)
    print('Timecode is', get_timecode(text))
    

    【讨论】:

    • 向我们展示您当前正在运行的代码。我想那里有一个print( ... ) 声明,其中显示了您在问题中包含的九行。
    • 向我们展示您正在运行的调用 telnetlib 的代码。 (你用 telnetlib 标记了你的问题,所以你肯定是在导入它。)
    • 好的,我现在添加了几行,将时间码作为最后一段文本返回,我只是不知道如何从其余数据中提取...我得到的回报是b'500 connection info:\r\nprotocol version: 1.9\r\nmodel: HyperDeck Studio Mini\r\n\r\n208 transport info:\r\nstatus: stopped\r\nspeed: 0\r\nslot id: 1\r\nclip id: 1\r\nsingle clip: false\r\ndisplay timecode: 00:00:11;00'
    • 那里有一个字节串。要恢复 unicode 文本,有必要对字节进行解码,就像我所做的那样:text = bytes.decode()。然后你可以将字符串传递给get_timecode,它将解析出相关的行。
    • if var = 'timecode': 的 = 语法无效
    【解决方案2】:

    在大多数情况下,这是我至少要部分工作的内容

    TCi = 1
    TCo = 1
    def In():
        global TCi
        session.write(b"transport info \n")
        by = session.read_until(b";00",.5)
        print(by)
        s = by.find(b"00:")
        TCi = by[s:s+11]
    def Out():
        global TCo
        session.write(b"transport info \n")
        by = session.read_until(b";00",.5)
        print(by)
        s = by.find(b"00:")
        TCo = by[s:s+11]
    def IOplay():
        IOtc = "playrange set: in: " + str(TCi) + " out: " + str(TCo) + " \n"
        print(IOtc.encode() )
        session.write(IOtc.encode() )
        speed = "play: speed: 2 \n"
        session.write(speed.encode() )
    

    除了它的编码为

    b"playrange set: in: b'00:00:01;11' out: b'00:00:03;10' \n"
    

    而不是

    "playrange set: in: 00:00:01;11 out: 00:00:03;10 \n"
    

    我需要去掉变量前面的撇号和 b 前缀

    有什么想法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-12
      • 2021-04-01
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多