所以我问了一个关于从seal流运行多个正则表达式的问题。python regular expression for multiple string values
现在我不得不从Telnet流中获取数据,而不是直接串行连接
这是一种工作,我能够得到结果张贴到我的数据库,并得到两个可用的正则表达式工作。在
问题是它缺少的部分,所以有时它的Telent流输出7行新的更新,我写的代码会拿起第一行,错过其余的,似乎是它挂在什么东西上,或者只是它不能处理Telnet流和发布到数据库足够快。在
这是目前为止的代码。出于安全考虑,import serial, string, MySQLdb, re, pdb, telnetlib
db = MySQLdb.connect(host="localhost", user="root", passwd="", db="walnut_farm")
cur = db.cursor()
HOST = "this is the ip or the telnet server i have removed"
PORT = "7001"
p = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)S(\d*)C(\d*\.?\d*)(.*)') # Wind Speed and Temp
p2 = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)D(\d*)(.*)') # Digital Output
def Push_Results_To_Database():
# Assigning variables to the array values
Base_ID = Serial_Results.group(1)
Base_Time_Stamp = Serial_Results.group(2)
Base_Signal = Serial_Results.group(3)
Repeater_ID = Serial_Results.group(4)
Repeater_Time_Stamp = Serial_Results.group(5)
Repeater_Signal = Serial_Results.group(6)
Sensor_ID = Serial_Results.group(7)
Sensor_Sequence = Serial_Results.group(8)
Sensor_Input_Voltage = Serial_Results.group(9)
if not digital_out:
Sensor_Wind_Speed = Serial_Results.group(10)
Sensor_Temperature = Serial_Results.group(11)
Checksum = Serial_Results.group(12)
# Execute the SQL query to INSERT the above variables into the Database
cur.execute('INSERT INTO serial_values (Base_ID, Base_Time_Stamp, Base_Signal, Repeater_ID, Repeater_Time_Stamp, Repeater_Signal, Sensor_ID, Sensor_Sequence, Sensor_Input_Voltage, Sensor_Wind_Speed, Sensor_Temperature, Checksum) VALUES ("'+Base_ID+'", "'+Base_Time_Stamp+'", "'+Base_Signal+'", "'+Repeater_ID+'", "'+Repeater_Time_Stamp+'", "'+Repeater_Signal+'", "'+Sensor_ID+'", "'+Sensor_Sequence+'", "'+Sensor_Input_Voltage+'", "'+Sensor_Wind_Speed+'", "'+Sensor_Temperature+'", "'+Checksum+'")')
db.commit()
if digital_out:
Sensor_Digital_Input = Serial_Results.group(10)
Checksum = Serial_Results.group(11)
# Execute the SQL query to INSERT the above variables into the Database
cur.execute('INSERT INTO serial_values (Base_ID, Base_Time_Stamp, Base_Signal, Repeater_ID, Repeater_Time_Stamp, Repeater_Signal, Sensor_ID, Sensor_Sequence, Sensor_Input_Voltage, Sensor_Digital_Input, Checksum) VALUES ("'+Base_ID+'", "'+Base_Time_Stamp+'", "'+Base_Signal+'", "'+Repeater_ID+'", "'+Repeater_Time_Stamp+'", "'+Repeater_Signal+'", "'+Sensor_ID+'", "'+Sensor_Sequence+'", "'+Sensor_Input_Voltage+'", "'+Sensor_Digital_Input+'", "'+Checksum+'")')
db.commit()
try:
tn = telnetlib.Telnet(HOST,PORT)
except:
print('Port Error!')
else:
while True:
#pdb.set_trace()
tn.write('ls \r\n')
data = ''
while data.find('#') == -1:
data = tn.read_very_eager()
ardString = data
print data
Serial_Output = ardString
#pdb.set_trace()
#for Serial_Output in Serial_Output.splitlines():
if re.match(p, Serial_Output):
print "Match For Wind Speed and Temperature"
digital_out = False
Serial_Results = re.match(p, Serial_Output)
Push_Results_To_Database()
continue
elif re.match(p2, Serial_Output):
print "Match for Digital Output"
digital_out = True
Serial_Results = re.match(p2, Serial_Output)
Push_Results_To_Database()
continue
else:
continue
#[1] `ATC001` = Base_ID
#[2] `1412010472` = Base_Time_Stamp (EPOC TIME)
#[3] `77` = Base_Signal
#[4] `ATC005` = Repeater_ID
#[5] `1412010460` = Repeater_Time_Stamp
#[6] `70` = Repeater_Signal
#[7] `SU0003` = Sensor_ID
#[8] `6` = Sensor_Sequence
#[9] `8.9` = Sensor_Input_Voltage
#[10] `0` = Sensor_Wind_Speed
#[11] `11.5` = Sensor_Temperature
#[12] `*xx` = Checksum
# http://regex101.com/r/dP6fE1/1
所以在发布这篇文章后,我已经解决了我的问题,我会留下来听听,以防对其他人有所帮助。在
这是上面的代码块
tn.write('ls \r\n') data = '' while data.find('#') == -1: data = tn.read_very_eager() ardString = data print data Serial_Output = ardString
;现在我又加了一句tn.写入('ls\r\n')位于telnet读取行之下。在
数据=非常渴望阅读() tn.写入('ls\r\n')
因此,我并不需要返回一个新行,因为telnet会话的输出已经在执行此操作,但是它会停止对telnet行的读取,因此它不会挂起。在
希望这是有意义的,并帮助某些人。 如果我犯了错误或者需要帮助,请给我留言。在