openstack中的cfg配置文件使用

本文档介绍了如何在OpenStack中使用cfg配置文件,通过oslo_config库定义和注册选项,如bind_host和bind_port,并演示了从配置文件读取信息的方法。示例中展示了如何获取并打印监听的主机IP和端口号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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))

成功的读取了配置文件的信息,若没有指定配置文件,则读取默认的配置信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值