作者在 2016-09-13 18:14:50 发布以下内容
#-*- coding:UTF-8 -*-
import sys, unittest, xmlrpclib, time, socket, select, multiprocessing , random ,sqlite3,re
from main import database
import esl_init
from main import create_task_data
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import os
from xml.dom import minidom
server = xmlrpclib.ServerProxy('http://127.0.0.1:9000')
# db = database.DB()
class ReadXml(object):
def r(self,elementfirstnode,num,elementsecondnode):
# dom = minidom.parse("D:\\testdata.xml")
dom = minidom.parse("D:\\netlink.xml")
root = dom.documentElement
parnote = root.getElementsByTagName(elementfirstnode)[num]
secondnote = parnote.getElementsByTagName(elementsecondnode)[0].childNodes[0].nodeValue
return secondnote
def s(self,type,cmd):
ret,val = server.send_cmd(type,cmd)
return ret,val
def sql(self,cadillac):
db1 = 'E:\esl-working-1.3.7_rc2\db.sqlite' #1、制定數據庫路徑
connection=sqlite3.connect(db1)
cursor=connection.cursor() #2、創建數據遊標
cursor.execute(cadillac) #3、執行查詢語句
cursor.close()
connection.commit() #4、提交請求,如果不提交是不會真正插入數據庫中的
def b(a): #数据库比较
db1 = 'E:\esl-working-1.3.7_rc2\db.sqlite'
connection=sqlite3.connect(db1)
cursor=connection.cursor()
# for i in cursor.execute("select setchn,grpchn,nw3 from esl_list where eslid='54-72-C0-99'"):
# for i in cursor.execute("select setchn,grpchn,nw3 from esl_list where eslid= (%s)",a):
for i in cursor.execute('select setchn,grpchn,nw3 from esl_list where eslid=' + a+''):
# print i,type(i)
if '170' in i:
s = list(i)
print s,type(s)
print s[0],s[1],s[2] #取list中的第一个数组
return 'OK' #返回一个比较的字符串
def clearlog(self): #清空log模块
f = open(r'E:\esl-working-1.3.7_rc2\log\log.txt','w')
time.sleep(1)
f.close()
time.sleep(2)
def cs(self): #找log里面返回的相关行
re_str = '.*{(.*)}'
tmp = []
findstr = ('success','ESL_NETLINK_ACK')
with open('E:\\esl-working-1.3.7_rc2\log\log.txt') as cadillac:
try:
for line in cadillac:
for s in findstr:
if s in line:
tmp = str(line)
re_pat = re.compile(re_str)
search_ret = re_pat.search(tmp)
if search_ret:
search_ret.groups()
loginfo = eval('{'+search_ret.groups().__str__()[2:-3]+ '}')
return loginfo
finally:
pass
def incmplog(self,nw3,setchn,grpchn): #输入的值和log值比较
retstr = self.cs()
if retstr['status'] != 'OK':
return 'failed'
else:
if retstr['nw3'] == nw3 and retstr['setchn'] == int(setchn) and retstr['grpchn'] == int(grpchn):
# print 'OK'
return 'OK'
else:
print 'not found key'
def incmpdb(self,toyota): #数据库中的值和log值比较
retstr = self.cs()
db1 = 'E:\esl-working-1.3.7_rc2\db.sqlite'
connection=sqlite3.connect(db1)
cursor=connection.cursor()
# for dv in cursor.execute('select nw3,setchn,grpchn from esl_list where eslid=' + toyota+''): b("\'54-72-C0-99\'")
try:
for dv in cursor.execute("select nw3,setchn,grpchn from esl_list where eslid in ('"+toyota+"')"):
s = list(dv)
if int(s[2]) == retstr['grpchn'] and s[0] == retstr['nw3'] and int(s[1]) == retstr['grpchn'] :
# print 'OK'
return 'OK'
else:
print 'key not in database'
finally:
pass
# def b(a): #数据库比较
# db1 = 'E:\esl-working-1.3.7_rc2\db.sqlite'
# connection=sqlite3.connect(db1)
# cursor=connection.cursor()
# # for i in cursor.execute("select setchn,grpchn,nw3 from esl_list where eslid='54-72-C0-99'"):
# # for i in cursor.execute("select setchn,grpchn,nw3 from esl_list where eslid= (%s)",a):
# # for i in cursor.execute('select setchn,grpchn,nw3 from esl_list where eslid=' + a+''):
# for i in cursor.execute('select nw3,setchn,grpchn from esl_list where eslid=' + a+''):
# print i
#
# if '170' in i:
# s = list(i)
# print s,type(s)
# print s[0],s[1],s[2] #取list中的第一个数组
# return 'OK' #返回一个比较的字符串
#
# cursor.close()
# connection.commit()
#
if __name__ == '__main__':
# P = ReadXml()
# n = "198"
# s = "198"
# g = "198"
# esl = "54-72-C0-99"
# P.clearlog()
# server.send_cmd('ESL_NETLINK', [{"eslid":esl, "nw1":"51-01-03-66", "nw3":n,"op2":"5", "setchn":s, "grpchn":g}])
# time.sleep(30)
# P.cs()
#
# P.incmplog(n,s,g)
# P.incmpdb(esl)
print 'only this module can be used'