从主机名中提取域名

发布于 2024-07-19 03:27:25 字数 232 浏览 3 评论 0原文

是否有一种编程方法可以从给定的主机名查找域名?

给定 -> www.yahoo.co.jp 返回-> yahoo.co.jp

有效但速度非常慢的方法是:

在“.”上拆分。 并从左侧删除 1 个组,使用 dnspython 加入并查询 SOA 记录 当返回有效的 SOA 记录时,请考虑域

是否有更干净/更快的方法来执行此操作而不使用正则表达式?

Is there a programatic way to find the domain name from a given hostname?

given -> www.yahoo.co.jp
return -> yahoo.co.jp

The approach that works but is very slow is:

split on "." and remove 1 group from the left, join and query an SOA record using dnspython
when a valid SOA record is returned, consider that a domain

Is there a cleaner/faster way to do this without using regexps?

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

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

发布评论

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

评论(3

夏花。依旧 2024-07-26 03:27:25

对于哪个“域名”是任何特定“主机名”的父级,没有简单的定义。

您当前遍历树直到看到 SOA 记录的方法实际上是最正确的。

从技术上讲,您所做的就是找到“区域削减”,并且在绝大多数情况下,这将对应于域名从其顶级域名 (TLD) 委托的点。

任何仅依赖主机名文本解析而不参考 DNS 的方法都注定会失败。

或者,使用 http://publicsuffix.org/ 中集中维护的以委派为中心的域列表,但是请注意,这些列表可能不完整和/或过时。

另请参阅这个问题,其中所有这件事之前已经讲过了……

There's no trivial definition of which "domain name" is the parent of any particular "host name".

Your current method of traversing up the tree until you see an SOA record is actually the most correct.

Technically, what you're doing there is finding a "zone cut", and in the vast majority of cases that will correspond to the point at which the domain was delegated from its TLD.

Any method that relies on mere text parsing of the host name without reference to the DNS is doomed to failure.

Alternatively, make use of the centrally maintained lists of delegation-centric domains from http://publicsuffix.org/, but beware that these lists can be incomplete and/or out of date.

See also this question where all of this has been gone over before...

反目相谮 2024-07-26 03:27:25

您可以使用 partition 而不是 split< /code>:

>>> 'www.yahoo.co.jp'.partition('.')[2]
'yahoo.co.jp'

这将有助于解析,但显然不会检查返回的字符串是否是有效的域。

You can use partition instead of split:

>>> 'www.yahoo.co.jp'.partition('.')[2]
'yahoo.co.jp'

This will help with the parsing but obviously won't check if the returned string is a valid domain.

北风几吹夏 2024-07-26 03:27:25

您的算法是正确的。 由于区域切割反映在域名中(您看到域切割 - 点 - 但不是区域切割),因此它是唯一正确的。

一种近似算法是使用区域列表,就像 Alnitak 提到的那样。 请记住,这些静态列表不具有权威性,它们缺乏许多注册表,它们已经过时等。

Your algorithm is the right one. Since zone cuts are not reflected in the domain name (you see domain cuts - the dots - but not zone cuts), it is the only correct one.

An approximate algorithm is to use a list of zones, like the one mentioned by Alnitak. Remember that these static lists are not authoritative, they lack many registries, they are stale, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文