python解析ini文件信息

方法一:根据open函数读取文件进行正则匹配 


def get_servpidmapname():
    servPidInfoList = []
    ostype = get_ostype()
    if ostype != "Linux":
        f = open(WINDOWS_SERVER_PID_LIST,'r')
        count = len(f.readlines())
        #print("get count from conf = %s" % (count))
        f.close()
    i = 0
    while i< count:
        #读取mode+1行
        list = []
        i=i+1
        if ostype != "Linux":
            modestr = linecache.getline(WINDOWS_SERVER_PID_LIST,i+1)
            # print("get modestr from conf = %s" % (modestr))
            if modestr.strip() == "":
                continue
            #查找该行key对应的name get str from conf = ['[process]\n']
            str = modestr.split("=", -1)
            servstrName= str[0].strip() #name
            servstrPid= str[1]  #pid
            servstr1 = re.split('\n', servstrPid)
            # print("get servstrPid from conf name:%s, pid:%s" % (servstrName, servstr1[0]))
            proctime = int(time.time())
            try:
                ptmp = psutil.Process(int(servstr1[0]))
                if ptmp.status() == "running":
                    pidStatus = 1
                else:
                    pidStatus = 2
                #print("get serverpid servPidInfoList:%s" % (servPidInfoList))
            except Exception as e:
                pidStatus = 2
                # print("get proc pid status info error")
            list.append(pidStatus)
            list.append(servstrName)
            servPidInfoList.append(list)
    print("get servPidInfoList:%s" % (servPidInfoList))
    return servPidInfoList
[procinfo]
parking_3000 = 80412
video_2010 = 33204
cabin_3020 = 16188

 结果:

get servPidInfoList:[[2, 'parking_3000', 1657534235], [2, 'video_2010', 1657534235], [2, 'cabin_3020', 1657534235]]

也可以先将key-value存放如数组列表中然后根据其key获取对应的value

"""
获取pid文件服务名及pid
"""
def get_servpidmapname():
    servPidInfoList = []
    ostype = get_ostype()
    if ostype != "Linux":
        f = open(WINDOWS_SERVER_PID_LIST,'r')
        count = len(f.readlines())
        f.close()
    i = 0
    try:
        while i< count:
            #读取mode+1行
            itemlist = []
            i=i+1
            if ostype != "Linux":
                modestr = linecache.getline(WINDOWS_SERVER_PID_LIST,i+1)
                if modestr.strip() == "":
                    continue
                #查找该行key对应的name get str from conf
                str = modestr.split("=", -1)
                # print("str:",str)
                servstrName = str[0].strip() #name
                servstrPid = int(str[1])  #pid
                itemlist.append(servstrName)
                itemlist.append(servstrPid)
                servPidInfoList.append(itemlist)
    except:
        print("get servpid error.")
    # print("get servPidInfoList:%s" % (servPidInfoList))
    return servPidInfoList

"""
根据服务名获取对应的pid
"""
def getpidfromname(pidlist,servtype):
    servPid = -1
    for name,pid in pidlist:
        if str(servtype) == str(name):
            servPid =  pid 
            break
    print("pidName:%s , pidtmp:%d##########" %(servtype,servPid))
    return servPid

方法二:直接根据configParser模块进行数据获取并过滤

configParser方法介绍:

# 以列表返回所有的section

 all_sections = config.sections()

# 以列表返回该section的所有键(key)

config.options("process") # 某个section 

# 以列表返回该section的所有属性(key-value)

config.items('process')

# 返回该section的Registry属性值得int类型

config.getint('process', "Registry")

# 返回该section的roost属性值的字符串形式

config.get('process', 'roost'))

完整版函数获取服务pid 

"""
根据服务名获取ini配置文件对应的pid
"""
def getprocpidini(servtype):
    servPid = -1
    try:
        #  实例化configParser对象
        config = configparser.ConfigParser()
        # read方法读取ini文件
        config.read(WINDOWS_SERVER_PID_LIST)
        sever_key = config.options('process')  # 以列表返回该section的所有键(key)
        log_info(LOG_LOCATE(),"get sever_key:%s" %(sever_key))
        for name in sever_key:#这里将参数遍历错了导致空值
            print("name:",str(name))
            if str(servtype).upper() == str(name).upper():#不区分大小写比较是否相等
                servPid = config.getint('process', str(name))  # 返回该section的pid属性值得int类型
                break # 获取到pid后直接退出循环
    except:
        log_info(LOG_LOCATE(),"ini get servpid error = %d." %(servPid))
    return servPid  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值