python 실행 c 프로그램 분석 xml 쓰기 텍스트 파일 통계 프로그램

#encoding:utf8
import os
import string
import re
import MySQLdb

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET


class sendXmlData():
    fileDir = '/opt/boyi/iftop/tmp'

    def __getPassengerIp(self):
        conn = MySQLdb.connect(host='localhost', user='root', passwd='')
        cursor = conn.cursor()
        conn.select_db('idc_database')
        cursor.execute('set names utf8')
        cursor.execute('select ip from passenger_table')
        ipList = cursor.fetchall()
        print '#' * 30 + 'ipList' + '#' * 30
        print ipList
        cursor.close()
        conn.close()
        return ipList


    #   
    def main(self):
        while True:
            listxml = self.__getXmlPath()

            if len(listxml) > 0:
                xmllist = []
                for _xmlpath in listxml:
                    xmlfileName = os.path.join(self.fileDir, _xmlpath)
                    print xmlfileName
                    xmlData = self.__returnxmlList(xmlfileName)
                    for xmlInfo in xmlData:
                        xmllist.append(xmlInfo.values())
                print xmllist
                self.writexmlInfo(xmllist)


    #                   
    def joinList(self, list):
        returnList = []
        for i in list:
            returnList.append(string.join(i, ','))
        return returnList


    #        ,         
    def writexmlInfo(self, xmllist):
        if os.path.exists('/tmp/trafficAnalysis.txt'):
            print 'open file'
            fp = open('/tmp/trafficAnalysis.txt', 'r+')
            fileList = fp.readlines()
            if len(fileList) == 0:
                fp.write(string.join(self.joinList(xmllist), '
'))                 fp.close()             else:                 newfileListinfo = []                 # xml                    newfileListinfo = self.__upateFileMsg(fileList, newfileListinfo, xmllist)                 # /tmp/trafficAnalysis                  newfileListinfo = self.__addOldFileMsg(fileList, newfileListinfo)                 # ip                 ipList = self.__getPassengerIp()                 print '#' * 40 + 'new fileinfo' + '#' * 40                 print newfileListinfo                 print '#' * 40 + 'filterNewList' + '#' * 40                 print self.filterNewList(newfileListinfo, ipList)                 fp.close()                 fp = open('/tmp/trafficAnalysis.txt', 'w')                 newlistinfo = self.filterNewList(newfileListinfo, ipList)                 fp.write(string.join(newlistinfo).replace(' ', ''))                 fp.close()                 os.system('unlink /opt/html/framework/trafficAnalysis.txt')                 os.system('cp /tmp/trafficAnalysis.txt /opt/html/framework/trafficAnalysis.txt')         else:             print 'touch file'             os.system('touch /tmp/trafficAnalysis.txt')     # ip      def filterNewList(self, newfileListinfo, ipList):         returnList = []         for newfileListinfoMsg in newfileListinfo:             for ipListMsg in ipList:                 if newfileListinfoMsg.find(',' + ipListMsg[0] + ',') != -1:                     returnList.append(newfileListinfoMsg)         return returnList     # xml        def __upateFileMsg(self, fileList, newfileListinfo, xmllist):         for xmlListInfo in xmllist:             i = -1             for fileListInfo in fileList:                 if fileListInfo.find(',' + xmlListInfo[1] + ',') != -1:                     i = 1                     filelistMsg = fileListInfo.split(',')                     if len(filelistMsg) > 4:                         filelistMsg[3] = str(int(filelistMsg[3]) + int(xmlListInfo[0]))                         filelistMsg[4] = str(int(filelistMsg[4]) + int(xmlListInfo[2]))                     else:                         filelistMsg.append(str(int(xmlListInfo[0]) + int(filelistMsg[0])))                         filelistMsg.append(str(int(xmlListInfo[2]) + int(filelistMsg[2].replace('
', ''))))                     filelistMsg[0] = xmlListInfo[0]                     filelistMsg[1] = xmlListInfo[1]                     filelistMsg[2] = xmlListInfo[2]                     newrow = string.join(filelistMsg, ',') + '
'                     newfileListinfo.append(newrow)             if i == -1:                 xmlListInfo[2] = xmlListInfo[2] + '
'                 newfileListinfo.append(string.join(xmlListInfo, ','))         return newfileListinfo     #     def __deleteDuplicatedElement(self, list):         resultList = []         for item in list:             if not item in resultList:                 resultList.append(item)         return resultList     # /tmp/trafficAnalysis      def __addOldFileMsg(self, fileList, newfileListinfo):         for fileListInfo in fileList:             j = -1             fileListMsg = fileListInfo.split(',')             for newfileList in newfileListinfo:                 if newfileList.find(',' + fileListMsg[1] + ',') != -1:                     j = 1             if j == -1:                 newfileListinfo.append(fileListInfo)         return newfileListinfo     # xml      def __getXmlPath(self):         filelist = os.listdir(self.fileDir)         for fileName in filelist:             filepath = os.path.join(self.fileDir, fileName)             if os.path.isfile(filepath):                 os.remove(filepath)                 print filepath + ' removed!'         os.system('/opt/boyi/iftop/sbin/iftop')         return os.listdir(self.fileDir)     # xmlList      def __returnxmlList(self, fileName):         list = []         tree = ET.ElementTree(file=fileName);         root = tree.getroot()         for child_of_root in root:             list.append(child_of_root.attrib)         return list if __name__ == '__main__':     sendXmlData = sendXmlData()     sendXmlData.main()

좋은 웹페이지 즐겨찾기