chromebook 5 信息接口程序

Chromebook文件监控与响应
本文介绍了一个Python脚本,该脚本用于监控特定目录下Chromebook生成的文件,并根据这些文件触发数据库查询,随后创建包含查询结果的响应文件。

通过分析log中文件的读写,发现,需要有一个接口程序执行以下功能。

1 将文件夹挂载到ubuntu机器上

2 监测chromebook生成的文件,生成机器读取的request文件

流程图如下


示例程序

# -*- coding: utf-8 -*-
import os, time
import MySQLdb as mdb
import sys
import datetime

#request文件
path_to_watch = r'C:/sdt'
path_to_watch1 = r'C:/sdt/CQ_FVS/request'
path_to_watch2 = r'C:/sdt/CQ_Monitor/Request'
path_to_watch3 = r'C:/sdt/nft/Request'
path_to_watch4 = r'C:/sdt/CQ_Monitor/Handshake2'
path_to_watch5 = r'C:/sdt/CQ_Finalize/request'

#路径
before = dict ([(a, None) for a in os.listdir (path_to_watch)])
before1 = dict ([(b, None) for b in os.listdir (path_to_watch1)])
before2 = dict ([(c, None) for c in os.listdir (path_to_watch2)])
before3 = dict ([(d, None) for d in os.listdir (path_to_watch3)])
before4 = dict ([(e, None) for e in os.listdir (path_to_watch4)])
before5 = dict ([(f, None) for f in os.listdir (path_to_watch5)])

#连接 mysql: connect('ip','user','password','dbname') 将 con 设定为全局连接
con = mdb.connect('localhost', 'root','root', 'shopfloor');
with con:
#获取执行查询的对象
	cur = con.cursor()
while 1:
  time.sleep (2)
  '''
  #sdt
  after = dict ([(a, None) for a in os.listdir (path_to_watch)])
  added = [a for a in after if not a in before]
  if added: 
	#print "%s " .join (added)
	MLB = added[0][:-4]
	print "%s " %MLB
	#执行查询select 语句
	tmp_sql = "select * from mytable where MLB_SN= '%s'" % MLB
	cur.execute(tmp_sql)
	#使用 cur.rowcount 获取结果集的条数
	numrows = int(cur.rowcount)
	#循环 numrows 次,每次取出一行数据
	for i in range(numrows):
	#每次取出一行,放到 row 中,这是一个元组(MLB_SN,type)
		row = cur.fetchone()
	#直接输出两个元素
	print row[0], row[1]
	#创建文件
	a = open('D:/sd/%s'% added[0], 'wb')
	#写入信息
	write=["hello world\n","hello world\n"]
	a.writelines(write)
	a.close()
	'''
  
  #D:/sdt/CQ_FVS/request
  after1 = dict ([(b, None) for b in os.listdir (path_to_watch1)])
  added1 = [b for b in after1 if not b in before1]
  if added1: 
	#print "%s " .join (added1)
	MLB1 = added1[0][:-9]
	print "%s " %MLB1
	#执行查询select 语句
	tmp_sql1 = "select * from mytable where MB_NUM= '%s'" % MLB1
	cur.execute(tmp_sql1)
	#使用 cur.rowcount 获取结果集的条数
	numrows1 = int(cur.rowcount)
	#循环 numrows 次,每次取出一行数据
	for i in range(numrows1):
	#每次取出一行,放到 row 中,这是一个元组(MB_NUM,type)
		row1 = cur.fetchone()
	#直接输出两个元素
	print row1[1], row1[2]
	#创建文件
	b = open('C:/sdt/CQ_FVS/response/%s'% added1[0], 'wb')
	#时间
	now = datetime.datetime.now()
	time1 = now.strftime('%Y%m%d%H%M%S') 
	#写入信息
	#write1 = ["hello %s\n"% row1[0],"hello world\n"]
	write1 = ["SET MB_NUM=%s\r\n"% row1[1],
        "SET CheckResult=PASS",
        "\r\n",
	"SET ReturnRepair=N\r\n",
	"SET Line=%s\r\n"% row1[9],
	"SET Repaired=N\r\n",
	"SET FULLTEST=N\r\n",
	"SET Date=%s\r\n"% time1,
	"SET IsRMAPart=%s\r\n"% row1[6],
	"SET User_code=%s\r\n"% row1[12],
	"SET Group_code=%s\r\n"% row1[13]]
	b.writelines(write1)
	b.close()
  #D:/sdt/CQ_Monitor/Request
  after2 = dict ([(c, None) for c in os.listdir (path_to_watch2)])
  added2 = [c for c in after2 if not c in before2]
  if added2: 
	#print "%s " .join (added2)
	MLB2 = added2[0][:-12]
	print "%s " %MLB2
	#执行查询select 语句
	tmp_sql2 = "select * from mytable where MB_NUM= '%s'" % MLB2
	cur.execute(tmp_sql2)
	#使用 cur.rowcount 获取结果集的条数
	numrows2 = int(cur.rowcount)
	#循环 numrows 次,每次取出一行数据
	for i in range(numrows2):
	#每次取出一行,放到 row 中,这是一个元组(MB_NUM,type)
		row2 = cur.fetchone()
	#直接输出两个元素
	print row2[0], row2[1]
	#创建文件
	c = open('C:/sdt/CQ_Monitor/Response/%s'% added2[0][:-3], 'wb')
	#写入信息
	#write2 = ["hello world\n","hello world\n"]
	write2 = ["SET SF_CFG_CHK=PASS\r\n",
	"SET MB_NUM=%s\r\n"% row2[1],
	"SET Brand_code=%s\r\n"% row2[2],
	"SET Cellular=%s\r\n"% row2[3],
	"SET Country=%s"%row2[4],
        "\r\n",
	"SET Customization_id=%s\r\n"% row2[5],
	"SET IsRMAPart=%s\r\n"% row2[6],
	"SET Keyboard=%s\r\n"% row2[7],
	"SET LTE=%s\r\n"% row2[8],
	"SET Line=%s\r\n"% row2[9],
	"SET SF_QCI_SN=%s\r\n"% row2[10],
	"SET Touchscreen=%s\r\n"% row2[11],
	"SET Group_code=%s"% row2[12],"\r\n",
	"SET User_code=%s"% row2[13],"\r\n",
	"SET MFG_PN=\r\n",
	"SET QCI_Model=\r\n",
	"SET Routing_Status=\r\n",
	"SET WO=\r\n",
	"SET SF_CFG_CHK=PASS"]
	c.writelines(write2)
	c.close()
  #D:/sdt/nft/Request
  after3 = dict ([(d, None) for d in os.listdir (path_to_watch3)])
  added3 = [d for d in after3 if not d in before3]
  if added3: 
	#print "%s " .join (added3)
	MLB3 = added3[0][:-12]
	print "%s " %MLB3
	#执行查询select 语句
	tmp_sql3 = "select * from mytable where MB_NUM= '%s'" % MLB3
	cur.execute(tmp_sql3)
	#使用 cur.rowcount 获取结果集的条数
	numrows3 = int(cur.rowcount)
	#循环 numrows 次,每次取出一行数据
	for i in range(numrows3):
	#每次取出一行,放到 row 中,这是一个元组(MB_NUM,type)
		row3 = cur.fetchone()
	#直接输出两个元素
	print row3[0], row3[1]
	#创建文件
	d = open('C:/sdt/nft/Response/%s'% added3[0][:-3], 'wb')
	#写入信息
	#write3 = ["hello world\n","hello world\n"]
	write3 = ["SET SF_CFG_CHK=PASS\r\n",
	"SET MB_NUM=%s\r\n"% row3[1],
	"SET SF_CFG_CHK=PASS\r\n"]
	d.writelines(write3)
	d.close()
  #D:/sdt/CQ_Monitor/Handshake2
  after4 = dict ([(e, None) for e in os.listdir (path_to_watch4)])
  added4 = [e for e in after4 if not e in before4]
  if added4: 
	#print "%s " .join (added4)
	MLB4 = added4[0][:-12]
	print "%s " %MLB4
	#执行查询select 语句
	tmp_sql4 = "select * from mytable where MB_NUM= '%s'" % MLB4
	cur.execute(tmp_sql4)
	#使用 cur.rowcount 获取结果集的条数
	numrows4 = int(cur.rowcount)
	#循环 numrows 次,每次取出一行数据
	for i in range(numrows4):
	#每次取出一行,放到 row 中,这是一个元组(MB_NUM,type)
		row4 = cur.fetchone()
	#直接输出两个元素
	print row4[0], row4[1]
	#创建文件
	e = open('C:/sdt/CQ_Monitor/HandResp2/%s'% added4[0][:-3], 'wb')
	#写入信息
	#write4 = ["hello world\n","hello world\n"]
	write4 = ["SET SF_QCI_SN=%s\r\n"% row4[10],
	"SET SF_CFG_CHK=PASS\r\n"]
	e.writelines(write4)
	e.close()
  #D:/sdt/CQ_Finalize/request
  after5 = dict ([(f, None) for f in os.listdir (path_to_watch5)])
  added5 = [f for f in after5 if not f in before5]
  if added5: 
	#print "%s " .join (added4)
	SN = added5[0][:-9]
	print "%s " %SN
	#执行查询select 语句
	tmp_sql5 = "select * from mytable where SF_QCI_SN= '%s'" % SN
	cur.execute(tmp_sql5)
	#使用 cur.rowcount 获取结果集的条数
	numrows5 = int(cur.rowcount)
	#循环 numrows 次,每次取出一行数据
	print numrows5
	for i in range(numrows5):
	#每次取出一行,放到 row 中,这是一个元组(MB_NUM,type)
		row5 = cur.fetchone()
	#直接输出两个元素
	print row5[1], row5[2]
	#创建文件
	f = open('C:/sdt/CQ_Finalize/response/%s'% added5[0], 'wb')
	#写入信息
	#write5 = ["hello world\n","hello world\n"]
	write5 = ["SET RESULT=PASS\r\n",
	"SET SF_QCI_SN=%s\r\n"% row5[10]]
	f.writelines(write5)
	f.close()
  #恢复文件信息
	
  before1 = after1
  before2 = after2
  before3 = after3
  before4 = after4
  before5 = after5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值