简体   繁体   English

如何过滤python telnet print()输出

[英]How to filter python telnet print() output

I have a few projectors that i control with a python script using telnetlib, which works fine. 我有几台投影机,可以使用telnetlib使用python脚本控制,效果很好。 I can also read the telnet output to get the current settings from them. 我还可以读取telnet输出以从中获取当前设置。

The issue i am having is that the telnet output gives me some unwanted characters and i would like to filter those out and show just the actual data. 我遇到的问题是telnet输出给我一些不需要的字符,我想过滤掉这些字符并仅显示实际数据。

#CURRENT SCRIPT
import time
import telnetlib
import os


port = 555
ipaddr = ('10.0.0.171',
          '10.0.0.172',
          '10.0.0.173',)
for ip in ipaddr:
    tn = telnetlib.Telnet(ip, port,)
    time.sleep(0.1)
    tn.write(b"*gamma ?\r")
    time.sleep(0.1)
    tn.write(b"*power ?\r")
    time.sleep(0.1)
    print(tn.read_eager(), end = ' ')
    print("on " + ip)
    print("----------------------------------------------------")
    tn.close
os.system("pause")

The output looks like this: 输出看起来像这样:

b'ack gamma = 3\r\nack power = 1\r\n' on 10.0.0.171
----------------------------------------------------
b'ack gamma = 3\r\nack power = 1\r\n' on 10.0.0.172
----------------------------------------------------
b'ack gamma = 3\r\nack power = 1\r\n' on 10.0.0.173
----------------------------------------------------
Press any key to continue . . .

which basically returns the ACK command and the data with \\r\\n at the end 它基本上返回ACK命令和最后带有\\ r \\ n的数据

is it possible to filter those out and put a delimiter between? 是否有可能过滤掉它们并在它们之间放置定界符? something like: 就像是:

| gamma = 3 | power = 1 | on 10.0.0.171
----------------------------------------------------
| gamma = 3 | power = 1 | on 10.0.0.172
----------------------------------------------------
| gamma = 3 | power = 1 | on 10.0.0.173
----------------------------------------------------
Press any key to continue . . .

Or if it is simpler to output these replies in a popup message box? 还是在弹出消息框中输出这些答复更简单? or highlight them (different color)? 或突出显示它们(不同的颜色)?

Working on Windows with Python 3.7.3 在Windows上使用Python 3.7.3

Any help would be much appreciated, Thanks 任何帮助将不胜感激,谢谢

David 大卫

You get an instance of the bytes built-in type from the Telnet.read_eager() method and must decode it into a string using the bytes.decode() method. 您可以从Telnet.read_eager()方法获取字节内置类型的实例,并且必须使用bytes.decode()方法将其解码为字符串。 Look at the following fragment. 查看以下片段。

ip = '10.1.1.30'
bs = b'ack gamma = 3\r\nack power = 1\r\n'
print(type(bs))
s = bs.decode()
print(type(s))
s = s.replace('\r\n', ' | ')
print('| '+ s + 'on ' + ip)

Output: 输出:

<class 'bytes'>
<class 'str'>
| ack gamma = 3 | ack power = 1 | on 10.1.1.30

You can decode bytes to unicode string using decode : 您可以使用decode将字节解码为unicode字符串:

In [1]: bs = b'hello world'

In [2]: bs
Out[2]: b'hello world'

In [3]: bs.decode('utf-8')
Out[3]: 'hello world'

And string can be encoded into bytes the same way using encode : 并且可以使用encode以相同的方式将字符串编码为字节:

In [4]: s = '123456'

In [5]: s.encode()
Out[5]: b'123456'

Then along with regex to grab your desired information. 然后与正则表达式一起获取所需的信息。 For example: 例如:

import re
s = b'ack gamma = 3\r\nack power = 1\r\n on 10.0.0.171'.decode('utf-8')
ip_addr = re.findall(r'\d+\.\d+\.\d+\.\d+', s)[0] # 10.0.0.171

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM