[英]How do I extract the hostname and the (sub)domain from a FQDN in Python?
我正在嘗試編寫一個腳本,該腳本將采用 FQDN 並為我提供主機名和(子)域。
我能夠獲取主機名,但我不知道如何獲取整個域,包括任何子域。
請注意,這些域和子域將是內部域,而不是公共域。
import re
words = "testing.something.thisdomain.com"
stuff = re.match(r"(.+?)(?=\.)", words)
print(stuff.group(1))
即使您的 fqdn 沒有任何子域,這也有效
代碼:
fqdn = "testing.something.thisdomain.com"
tld, domain, *sub_domains = fqdn.split(".")[::-1]
print(tld,domain,sub_domains)
Output:
com thisdomain ['something', 'testing']
我們不能只在字符串上使用 split 並打印您需要的部分,而不是做一堆花哨的正則表達式嗎?
words = "testing.something.thisdomain.com"
stuff = words.split(".")
print(stuff[1])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.