How do I resolve an SRV record in Python?

2019-01-26 10:05发布

Something which doesn't rely on native libraries would be better.

4条回答
The star\"
3楼-- · 2019-01-26 10:37

Using 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']
查看更多
不美不萌又怎样
4楼-- · 2019-01-26 10:49

Using 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}
查看更多
等我变得足够好
5楼-- · 2019-01-26 10:51

twisted has an excellent pure-python implementation, see twisted.names sources (especially dns.py). If you can't use all of their code, maybe you can extract and repurpose their Record_SRV class from that file.

查看更多
登录 后发表回答