任何用于解析绑定区域文件的 python 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/236859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any python libs for parsing Bind zone files?
提问by daniels
Any python libs for parsing Bind zone files? Basically something that will aid in adding/removing zones and records. This needs to work even if someone modifies the zone file by hand so overwriting the zone files every time is not a solution.
任何用于解析绑定区域文件的 python 库?基本上可以帮助添加/删除区域和记录的东西。即使有人手动修改区域文件,这也需要工作,因此每次都覆盖区域文件不是解决方案。
采纳答案by NorbertK
I was unable to use bicop for classical zone files like these:
我无法将 bicop 用于像这样的经典区域文件:
$TTL 86400
@ IN SOA ns1.first-ns.de. postmaster.robot.first-ns.de. (
2006040800 ; serial
14400 ; refresh
1800 ; retry
604800 ; expire
86400 ) ; minimum
@
IN NS ns1.first-ns.de.
I will have a look at dnspython
我会看看dnspython
回答by Boden Garman
回答by jccnu619
I know this is old but the only working one I could find is called iscpy. You can do an easy_install.
我知道这很旧,但我能找到的唯一有效的方法是 iscpy。你可以做一个easy_install。
easy_install iscpy
Then in python:
然后在python中:
import iscpy
iscpy.ParseISCString(open('somefile.conf', 'r').read())
Which returns a dictionary.
它返回一个字典。
回答by Michael Gundlach
See answer above about bicop.
请参阅上面关于 bicop 的答案。
As an aside, the Python Package Index at http://pypi.python.org/pypiis a great place to look for Python packages.
顺便说一句,http://pypi.python.org/pypi 上的 Python 包索引是查找 Python 包的好地方。
EDIT: The below may still be helpful to someone trying to figure out simple parsing, but bicop is apparently an existing solution.
编辑:下面的内容可能对试图找出简单解析的人仍然有帮助,但 bicop 显然是一个现有的解决方案。
If someone has modified the config by hand, and you don't want to overwrite it, does that imply that you wish to insert/remove lines from an existing config, leaving all comments etc intact? That does prevent parsing then re-outputting the config, but that's a positive as well -- you don't need to fully parse the file to accomplish your goal.
如果有人手动修改了配置,而您不想覆盖它,这是否意味着您希望从现有配置中插入/删除行,保留所有注释等?这确实会阻止解析然后重新输出配置,但这也是积极的——您不需要完全解析文件来实现您的目标。
To add a record, you might try a simple approach like
要添加记录,您可以尝试一种简单的方法,例如
# define zone_you_care_about and line_you_wish_to_insert first, then:
for line in bindfile.read():
out.write(line + '\n')
if ('zone "%s" in' % zone_you_care_about) in line:
out.write(line_you_wish_to_insert)
Similar code works for removing a line:
类似的代码适用于删除一行:
# define zone_you_care_about and relevant_text_to_remove, then:
for line in bindfile.read():
if not relevant_text_to_remove in line:
out.write(line + '\n')
You may get as far as you need with simple snippets of code like this.
您可以使用像这样的简单代码片段来达到您的需要。