[root@tencent-cloud python]# cat /etc/hosts
127.0.0.1 VM-4-4-centos VM-4-4-centos
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4
::1 VM-4-4-centos VM-4-4-centos
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
#test started
1.1.1.1 test1
2.2.2.2 test2
#test stopped
[root@tencent-cloud python]# cat read_hosts.py
#!/usr/bin/python
r=open("/etc/hosts")
#print(r.readlines()[0].split(" ")[0])
lines=r.readlines()
lst=[]
hosts=[]
for line in lines:
if line.startswith("#test started") or line.startswith("#test stopped"):
lst.append(lines.index(line))
print(str(lines.index(line))+"==>"+line)
print(lst)
print(">"*20)
for line in lines:
if lines.index(line) > lst[0] and lines.index(line) < lst[1]:
hosts.append(line)
print(hosts)
[root@tencent-cloud python]# python read_hosts.py
8==>#test started
11==>#test stopped
[8, 11]
>>>>>>>>>>>>>>>>>>>>
['1.1.1.1 test1n', '2.2.2.2 test2n']
[root@tencent-cloud python]#