配置文件内容
[root@xen-monitor xenscript]# cat test.conf
[db]
db_host=127.0.0.1
db_port=3306
db_user=root
db_pass=password
[concurrent]
thread=10
processor=20
测试脚本
[root@xen-monitor xenscript]# cat test.py
#!/usr/bin/python -O
import ConfigParser
import string, os, sys
cf = ConfigParser.ConfigParser()
cf.read('test.conf')
s = cf.sections()
print "sections",s
o = cf.options("db")
print 'options:', o
v = cf.items("db")
print 'db:', v
print '-'*60
db_host = cf.get("db", "db_host")
db_port = cf.getint("db", "db_port")
db_user = cf.get("db", "db_user")
db_pass = cf.get("db", "db_pass")
print '*'*80
print "db_host:%s,db_pass:%s,db_user:%s,db_port:%s" % (db_host,db_pass,db_user,db_port)
脚本执行结果
[root@xen-monitor xenscript]# python test.py
sections ['concurrent', 'db']
options: ['db_port', 'db_user', 'db_host', 'db_pass']
db: [('db_port', '3306'), ('db_user', 'root'), ('db_host', '127.0.0.1'), ('db_pass', 'password')]
------------------------------------------------------------
********************************************************************************
db_host:127.0.0.1,db_pass:password,db_user:root,db_port:3306
阅读(1785) | 评论(0) | 转发(0) |