Tuesday, September 28, 2010

Split URL to sub domains using Python

Hi again,
For my new and exciting project (hopefully more on this later) I needed to get all sub domains possibilities of a URL, so if this is the input URL:
http://a.b.c.d/test.html
I needed to get the following sub domains:
['a.b.c.d', 'b.c.d', 'c.d', 'd']
Here's the good news: this is pretty simple with Python ! :)
import urlparse
url='http://a.b.c.d/test.html'
hostname=urlparse.urlparse(url).hostname
sh=hostname.split('.')
subdomains=[".".join(sh[i:]) for i in range(len(sh))]
print subdomains
>> ['a.b.c.d', 'b.c.d', 'c.d', 'd']
Nice, isn't it?