Python学习之PyYaml模块
我需要解析twemproxy的配置文件中的pool和后端的redis主机。采用shell解析yml文件太麻烦,正好python有正对于yaml文件解析的pyyaml模块例如twemporxy的配置文件nutcracker.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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.
参考文档:
http://pyyaml.org/wiki/PyYAMLDocumentation
页:
[1]