Skip to content

Instantly share code, notes, and snippets.

@ytjohn
Created November 14, 2012 20:29
Show Gist options
  • Save ytjohn/4074564 to your computer and use it in GitHub Desktop.
Save ytjohn/4074564 to your computer and use it in GitHub Desktop.
quick hostname reversal
# This is very useful for sorting hostnames.
def revhost(host):
""" split a hostname into segments, reverse them, and then rejoin them """
segments = host.split('.')
segments.reverse()
reversed = ".".join(segments)
return reversed
domain = 'a.ns.example.com'
rdomain = revhost(domain)
print domain, rdomain
# a.ns.example.com com.example.ns.a
ndomain = revhost(rdomain)
print domain, ndomain
# a.ns.example.com a.ns.example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment