如何在Python中解析SRV记录?

11

最好不依赖本地库的内容更好。

4个回答

11

4
谢谢! 导入dns.resolveranswers = dns.resolver.query('_xmpp-server._tcp.gmail.com', 'SRV') for rdata in answers: print str(rdata) - Gili Nachum
2
这只是一个链接答案。 - oz123

8

使用dnspython

>>> import dns.resolver
>>> domain='jabberzac.org'
>>> srvInfo = {}
>>> srv_records=dns.resolver.query('_xmpp-client._tcp.'+domain, 'SRV')
>>> for srv in srv_records:
...     srvInfo['weight']   = srv.weight
...     srvInfo['host']     = str(srv.target).rstrip('.')
...     srvInfo['priority'] = srv.priority
...     srvInfo['port']     = srv.port
... 
>>> print srvInfo
{'priority': 0, 'host': 'xmpp.jabberzac.org', 'port': 5222, 'weight': 0}

7

Twisted拥有出色的纯Python实现,可以参考twisted.names源码,特别是dns.py。如果您无法使用他们的所有代码,也许可以从该文件中提取并重新使用其中的Record_SRV类。


链接已失效。 - BowlOfRed

1
使用pydns
import DNS
DNS.ParseResolvConf()
srv_req = DNS.Request(qtype = 'srv')
srv_result = srv_req.req('_ldap._tcp.example.org')

for result in srv_result.answers:
    if result['typename'] == 'SRV':
        print result['data']

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接