Extract domain name from a host name

那年仲夏 提交于 2019-12-03 01:57:06
Alnitak

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...

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!