views:

185

answers:

6

How would you split a domain name that will return name and extension

+1  A: 

You mean internet domain name, like www.stackoverflow.com? If so, then just use:

>>> 'www.stackoverflow.com'.rsplit('.', 1)
['www.stackoverflow', 'com']
Tomasz Zielinski
+2  A: 
domain = 'subdomain.domain.ext'
name, ext = domain.split('.')[-2:]
jellybean
Fails for http://www.google.co.uk/.
Roger Pate
+3  A: 

Depending on your application, be a little wary of simply taking the part following the last '.'. That works fine for .com, .net, .org, etc but will likely fall over for many County Code TLDs. E.g. bit.ly or google.co.uk.

(By which I mean 'bit.ly' probably prefer to be identified including the .ly TLD whereas google probably don't want to be identified with a spurious .co remainder. Whether that's important will obviously depend on what you're doing).

In those complicated cases ... well, you've got your work cut out I suspect!

A robust answer will probably depend on how you're gathering / storing your domains and what you really want back as the 'name'.

For example, if you've got a set of domain names, with no subdomain information, then you could do the opposite of what's suggested above and simply take the first part off:

>>> "stackoverflow.com".split('.')[0]
'stackoverflow'
pycruft
A: 

If you always want to get the last part of a domain name, you can:

subdomain, _, domain= fqdn.rpartition('.')
ΤΖΩΤΖΙΟΥ
A: 

I guess you will find urlparse module interesting: http://docs.python.org/library/urlparse.html

d.m
A: 

In general, it's not easy to work out where the user-registered bit ends and the registry bit begins. For example: a.com, b.co.uk, c.us, d.ca.us, e.uk.com, f.pvt.k12.wy.us...

The nice people at Mozilla have a project dedicated to listing domain suffixes under which the public can register domains: http://publicsuffix.org/

Andrew Aylett