11#! /usr/bin/env python
2- #
3- # Usage: world addr1 [addr2 ...]
4- #
5-
6- # This little script will take an Internet address of the form
7- # [email protected] and will print out where in the world that 8- # message originated from. Its pretty dumb in that it just matches
9- # the `domain' part against a hard-coded list, which can probably
10- # change fairly quickly given the world's political fluidity.
11-
12- # TBD: it would be cool if this script could update itself. I can't
13- # remember where I got the original list of top level domain
14- # abbreviations -- probably from the InterNIC. So far I haven't hit
15- # any that this script can't resolve, so I assume they don't change
16- # too frequently.
2+
3+ """Print the long name of an Internet domain.
4+
5+ This script will take an Internet address and print out where in the
6+ world that message originated from, based on the top-level domain code
7+ found in the address. Addresses can be in any of the following forms:
8+
9+ xx -- just the country code or top-level domain identifier
10+ host.domain.xx -- any Internet host or network name
11+ [email protected] -- an Internet email address 12+
13+ Country codes are maintained by the RIPE Network Coordination Centre,
14+ in coordination with the ISO 3166 Maintenance Agency at DIN Berlin.
15+
16+ <url:ftp://info.ripe.net/iso3166-countrycodes>
17+
18+ The latest known change to this information was:
19+
20+ Thu Feb 10 10:20:28 MET 1994
21+
22+ Usage: %s [-d] [-h] addr [addr ...]
23+
24+ -d (--dump) -- print mapping of all known top-level domains
25+ -h (--help) -- print this help message
26+
27+ """
1728
1829import sys
1930import string
31+ import getopt
2032
2133
22- def usage (msg = None , exit = 0 ):
23- if msg : print msg
24- print 'Usage:' , sys .argv [0 ], 'addr [addr ...]'
25- sys .exit (exit )
26-
34+
35+ def usage (status = 0 ):
36+ print __doc__ % sys .argv [0 ]
37+ sys .exit (status )
2738
2839def resolve (rawaddr ):
2940 parts = string .splitfields (rawaddr , '.' )
@@ -36,8 +47,39 @@ def resolve(rawaddr):
3647 elif country .has_key (addr ):
3748 print addr , 'originated from' , country [addr ]
3849 else :
39- print 'Where in the world is' , addr , '?'
50+ print 'Where in the world is %s?' % addr
51+
52+
53+
54+ def main ():
55+ help = 0
56+ status = 0
57+ dump = 0
4058
59+ opts , args = getopt .getopt (sys .argv [1 :], 'hd' , ['help' , 'dump' ])
60+ for arg , val in opts :
61+ if arg in ('-h' , '--help' ):
62+ help = 1
63+ elif arg in ('-d' , '--dump' ):
64+ dump = 1
65+
66+ if help :
67+ usage (status )
68+
69+ if dump :
70+ print 'USA-centric domains:'
71+ codes = nameorg .keys ()
72+ codes .sort ()
73+ for code in codes :
74+ print ' %4s:' % code , nameorg [code ]
75+
76+ print '\n Country coded domains:'
77+ codes = country .keys ()
78+ codes .sort ()
79+ for code in codes :
80+ print ' %2s:' % code , country [code ]
81+ else :
82+ map (resolve , args )
4183
4284
4385# The mappings
@@ -152,4 +194,4 @@ country = {
152194
153195
154196if __name__ == '__main__' :
155- map ( resolve , sys . argv [ 1 :] )
197+ main ( )
0 commit comments