我需要解析twemproxy的配置文件中的pool和后端的redis主机。采用shell解析yml文件太麻烦,正好python有正对于yaml文件解析的pyyaml模块
例如twemporxy的配置文件nutcracker.yaml
alpha: listen: 127.0.0.1:22121 hash: fnv1a_64 distribution: ketama auto_eject_hosts: true redis: true server_retry_timeout: 2000 server_failure_limit: 1 servers: - 127.0.0.1:6379:1 beta: listen: 127.0.0.1:22122 hash: fnv1a_64 hash_tag: "{}" distribution: ketama auto_eject_hosts: false timeout: 400 redis: true servers: - 127.0.0.1:6380:1 server1 - 127.0.0.1:6381:1 server2 - 127.0.0.1:6382:1 server3 - 127.0.0.1:6383:1 server4 gamma: listen: 127.0.0.1:22123 hash: fnv1a_64 distribution: ketama timeout: 400 backlog: 1024 preconnect: true auto_eject_hosts: true server_retry_timeout: 2000 server_failure_limit: 3 servers: - 127.0.0.1:11212:1 - 127.0.0.1:11213:1
我需要解析出各个pool名称和redis主机
alpha,beta,gamma
127.0.0.1:6380
127.0.0.1:6381
1.安装pyyaml
pip install pyyaml
2.使用pyyaml
使用pyyaml模块最多的就是load()和dump()两个函数
参考文档:
http://pyyaml.org/wiki/PyYAMLDocumentation