封装获取配置文件:
# config.ini
[logging]
name = zangai
level = DEBUG
filename = testlog.log
sh_level = DEBUG
fh_level = INFO
[env]
base_url = http://api.dsad.site:8000
[test_data]
username =
password =
[invest_user]
mobile=
pwd=
[test_admin]
mobile=
pwd=
[mysql]
host = api.lemonban.com
port = 3306
username =
password =
from configparser import ConfigParser
import os
from commons.handle_path import CONF_DIR
# handle_config
class Config(ConfigParser):
def __init__(self,conf_file):
super().__init__()
self.read(conf_file,encoding='utf-8')
conf=Config(os.path.join(CONF_DIR,'config.ini'))
print(conf.get("mysql", "host"))
print(conf.getint("mysql", "port"))
# class Read_Config:
# #定义一个读取配置的类
# def __init__(self, filepath=CONF_DIR + r'\config.ini'):
# self.cf = configparser.ConfigParser()
# self.cf.read(filepath)
#
# def get_config_str(self, section, option):
# return self.cf.get(section, option)
#
# def get_config_boolean(self, section, option):
# return self.cf.getboolean(section, option)
#
# def get_config_int(self, section, option):
# return self.cf.getint(section, option)
#
# def get_config_float(self, section, option):
# return self.cf.getfloat(section, option)
#
# if __name__ == '__main__':
# test = Read_Config().get_config_int("mysql", "port")
# print(test)
# print(type(test))