如何在Python中解析SRV记录?

时间:2009-07-27 16:40:11

标签: python networking dns srv

不依赖本地库的东西会更好。

4 个答案:

答案 0 :(得分:8)

twisted具有出色的纯python实现,请参阅twisted.names来源(尤其是dns.py)。如果您无法使用他们的所有代码,也许您可​​以从该文件中提取并重新调整他们的Record_SRV类。

答案 1 :(得分:7)

答案 2 :(得分: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']

答案 3 :(得分:1)

使用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}