comparison python/tld.py @ 812:0bc7c1a5eb83

make into an almost real CLI
author Jeff Hammel <k0scist@gmail.com>
date Fri, 28 Oct 2016 18:54:56 -0700
parents c7edd15d7f8b
children
comparison
equal deleted inserted replaced
811:0fffb3560641 812:0bc7c1a5eb83
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2
3 """
4 Top-Level Domain names (TLDs), country codes only
5 """
6
7 import argparse
8 import sys
9 import urllib2
2 10
3 URL = 'http://www.palosverdes.com/jesse/irc/country.txt' 11 URL = 'http://www.palosverdes.com/jesse/irc/country.txt'
4 12
5 import urllib2
6 13
7 def codes(): 14 def codes():
8 f = urllib2.urlopen(URL) 15 f = urllib2.urlopen(URL)
9 codes = {} 16 codes = {}
10 for line in f.readlines(): 17 for line in f.readlines():
14 key, value = line.split(None, 1) 21 key, value = line.split(None, 1)
15 codes[key.lower()] = value 22 codes[key.lower()] = value
16 return codes 23 return codes
17 codes = codes() 24 codes = codes()
18 25
26
19 def get(code): 27 def get(code):
20 code = code.lstrip('.').lower() 28 code = code.lstrip('.').lower()
21 return codes.get(code) 29 return codes.get(code)
22 30
31 def main(args=sys.argv[1:]):
32 """CLI"""
33
34 # parse command line
35 parser = argparse.ArgumentParser(description=__doc__)
36 parser.add_argument('tld', nargs='+',
37 help="country-code TLD to look up")
38 options = parser.parse_args(args)
39
40 # lookup + output
41 for arg in options.tld:
42 print ('{}: {}'.format(arg, get(arg)))
43
23 if __name__ == '__main__': 44 if __name__ == '__main__':
24 import sys 45 main()
25 for arg in sys.argv[1:]:
26 print '%s: %s' % (arg, get(arg))