0%

python配置解析--ConfigParser

配置文件的格式

1
2
3
4
5
6
7
8
[server]
bind = 127.0.0.1
log = /var/log/haha.log
workers = 4

[client]
connect = 127.0.0.1:9090
log = /var/log/hehe.log

使用的话就相当简单了

1
2
3
4
5
6
7
cp = ConfigParser.ConfigParser()
cp.read("etc/app.conf")
server.bind = cp.get("server", "bind")
server.workers = cp.getint("server","workers")
# 另外还有
# cp.getboolean()
# cp.getfloat()