gettlds.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. Fetch the current TLD list from the IANA Web site, parse it, and print
  3. an expression suitable for direct insertion into each library's trust
  4. root validation module
  5. Usage:
  6. python gettlds.py (php|python|ruby)
  7. Then cut-n-paste.
  8. """
  9. import urllib2
  10. import sys
  11. langs = {
  12. 'php': (r"'/\.(",
  13. "'", "|", "|' .",
  14. r")\.?$/'"),
  15. 'python': ("['",
  16. "'", "', '", "',",
  17. "']"),
  18. 'ruby': ("%w'",
  19. "", " ", "",
  20. "'"),
  21. }
  22. lang = sys.argv[1]
  23. prefix, line_prefix, separator, line_suffix, suffix = langs[lang]
  24. f = urllib2.urlopen('http://data.iana.org/TLD/tlds-alpha-by-domain.txt')
  25. tlds = []
  26. output_line = ""
  27. for input_line in f:
  28. if input_line.startswith('#'):
  29. continue
  30. tld = input_line.strip().lower()
  31. new_output_line = output_line + prefix + tld
  32. if len(new_output_line) > 60:
  33. print output_line + line_suffix
  34. output_line = line_prefix + tld
  35. else:
  36. output_line = new_output_line
  37. prefix = separator
  38. print output_line + suffix