#coding:utf-8 from oslo_config import cfg from oslo_config import types PortType = types.Integer(1, 65535) # 定义一组选项 common_opts = [ cfg.StrOpt('bind_host', default='0.0.0.0', help='IP address to listen on.'), cfg.Opt('bind_port', type=PortType, default=9292, help='Port number to listen on.') ] def add_common_opts(conf): # 注册选项 conf.register_cli_opts(common_opts) def get_bind_host(conf): # 使用选项 return conf.bind_host def get_bind_port(conf): return conf.bind_port # 创建配置类 cf = cfg.CONF # 开始注册 add_common_opts(cf) # cf(default_config_files=['config.conf']) print(get_bind_host(cf)) print(get_bind_port(cf))
成功的读取了配置文件的信息,若没有指定配置文件,则读取默认的配置信息