python telnet tn.read_until()用法

时间:2016-05-09 02:13:13

标签: python echo telnet netcat telnetlib

在设备telnet中从SPCI的输出中提取数字的正确方法是什么?有"测量:功率?"," -5.25031482E + 001"(这是我需要提取的),"空行",&#34 ; SCPI> &#34 ;.我有什么办法可以将代码改进为仅输出" -5.25031482E + 001"?

代码(我使用telnet ip_addr端口,然后执行tn.write()和tn.read_very_eager()来输出reasult):

import csv
from string import split
import sys
import telnetlib


Host = "192.168.1.10"
PORT = 5024
i = open('practice1.csv', 'wb')  
tn = telnetlib.Telnet(Host,PORT)
print "you logged in :)"

while True:
    time.sleep(1)
    tn.write("Measure:Power?\n")
    time.sleep(5)
    data = ' '
    #if tn.read_very_eager() != "" :
    if data.find('W') == -1:
        data = tn.read_very_eager()
        print data

结果(有SCPI>,空行和数字,我只想要号码):

x@x:~/Desktop/Keysight_N1914A$ python powermater.py 
you logged in :)
Welcome to A-N1914A-00010 - Agilent Technologies Agilent N1914A (SG56040006) SCPI parser.

SCPI> Measure:Power?

-5.25031482E+001
SCPI> 
-5.25028874E+001
SCPI> 
-5.25132798E+001
SCPI> 
-5.25077163E+001
SCPI> 

-5.24950509E+001
SCPI> 

-5.25008686E+001
SCPI> 
-5.25210976E+001
SCPI> 
-5.25151648E+001
SCPI> 
-5.25099852E+001
SCPI> 
^CTraceback (most recent call last):
  File "powermater.py", line 40, in <module>
    time.sleep(5)
KeyboardInterrupt

0 个答案:

没有答案