random script dump
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

punycode.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. import argparse
  3. import sys
  4. class Domain(object):
  5. def __init__(self, domain):
  6. """Constructor"""
  7. if sys.version_info[0] < 3:
  8. domain = domain.decode('utf-8').encode('utf-8')
  9. idna = domain.decode('utf-8').encode('idna')
  10. self.ascii = idna
  11. self.utf_8 = idna.decode('idna').encode('utf-8')
  12. else:
  13. idna = bytes(domain, 'idna')
  14. self.ascii = idna.decode('ascii')
  15. self.utf_8 = idna.decode('idna')
  16. if self.ascii == self.utf_8:
  17. self.type = 'ascii'
  18. self.utf_8 = None
  19. else:
  20. if self.utf_8 == domain:
  21. self.type = 'unicode'
  22. else:
  23. self.type = 'punycode'
  24. def display(self):
  25. print('INPUT: \x1B[1m{}\x1B[0m\nASCII: \x1B[1m{}\x1B[0m'
  26. .format(self.type, self.ascii))
  27. if self.utf_8 is not None:
  28. print('UTF-8: \x1B[1m{}\x1B[0m'.format(self.utf_8))
  29. def parse_arguments():
  30. """Parse and store arguments."""
  31. desc = 'A simple punycode to unicode and back converter.'
  32. parser = argparse.ArgumentParser(description=desc)
  33. parser.add_argument('domain', help='domain name to convert')
  34. # Store the supplied args
  35. return parser.parse_args()
  36. def main(main_sysargs):
  37. args = parse_arguments()
  38. domain = Domain(args.domain)
  39. domain.display()
  40. if __name__ == '__main__':
  41. main(sys.argv)