如何在 Python 中解析 SRV 记录?

发布于 2024-07-29 09:29:58 字数 20 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一身骄傲 2024-08-05 09:30:30

使用 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']

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']
筑梦 2024-08-05 09:30:25

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

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.

怪我入戏太深 2024-08-05 09:30:18

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

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}
じ违心 2024-08-05 09:30:14

您可以尝试 dnspython 库:

  • http://www.dnspython.org/examples.html
  • < a href="http://www.dnspython.org/docs/1.7.1/html/dns.rdtypes.IN.SRV.SRV-class.html" rel="noreferrer">http://www.dnspython。 org/docs/1.7.1/html/dns.rdtypes.IN.SRV.SRV-class.html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文