11#! /usr/bin/env python
22
3- """Print mappings between country names and DNS country codes.
3+ """world -- Print mappings between country names and DNS country codes.
4+
5+ Author: Barry Warsaw
6+ 7+ Version: %(__version__)s
48
59This script will take a list of Internet addresses and print out where in the
610world those addresses originate from, based on the top-level domain country
@@ -12,8 +16,8 @@ code found in the address. Addresses can be in any of the following forms:
1216
1317If no match is found, the address is interpreted as a regular expression [*]
1418and a reverse lookup is attempted. This script will search the country names
15- and printing a list of matching entries. You can force reverse mappings with
16- the `-r' flag (see below).
19+ and print a list of matching entries. You can force reverse mappings with the
20+ `-r' flag (see below).
1721
1822For example:
1923
@@ -47,7 +51,7 @@ The latest known change to this information was:
4751This script also knows about non-geographic top-level domains.
4852
4953
50- Usage: %s [-d] [-p|-P file] [-h] addr [addr ...]
54+ Usage: %(PROGRAM) s [-d] [-p file] [-o ] [-h] addr [addr ...]
5155
5256 --dump
5357 -d
@@ -57,12 +61,12 @@ Usage: %s [-d] [-p|-P file] [-h] addr [addr ...]
5761 -p file
5862 Parse an iso3166-countrycodes file extracting the two letter country
5963 code followed by the country name. Note that the three letter country
60- code and number , which are also provided in the standard format file,
61- are ignored.
64+ codes and numbers , which are also provided in the standard format
65+ file, are ignored.
6266
6367 --outputdict
6468 -o
65- With used in conjunction with the `-p' option, output is in the form
69+ When used in conjunction with the `-p' option, output is in the form
6670 of a Python dictionary, and country names are normalized
6771 w.r.t. capitalization. This makes it appropriate for cutting and
6872 pasting back into this file.
@@ -81,8 +85,6 @@ Usage: %s [-d] [-p|-P file] [-h] addr [addr ...]
8185
8286"""
8387__version__ = '$Revision$'
84- __author__ = 'Barry Warsaw <[email protected] >' 85- __source__ = '<url:http://www.python.org/~bwarsaw/pyware/>'
8688
8789
8890import sys
@@ -94,12 +96,15 @@ except ImportError:
9496 print sys .argv [0 ], 'requires Python 1.5'
9597 sys .exit (1 )
9698
99+ PROGRAM = sys .argv [0 ]
97100
98101
99102
100- def usage (status = 0 ):
101- print __doc__ % sys .argv [0 ]
102- sys .exit (status )
103+ def usage (code , msg = '' ):
104+ print __doc__ % globals ()
105+ if msg :
106+ print msg
107+ sys .exit (code )
103108
104109
105110
@@ -218,20 +223,24 @@ def main():
218223 normalize = 0
219224 forcerev = 0
220225
221- opts , args = getopt .getopt (
222- sys .argv [1 :],
223- 'p:rohd' ,
224- ['parse' , 'reverse' , 'outputdict' , 'help' , 'dump' ])
225- for arg , val in opts :
226- if arg in ('-h' , '--help' ):
226+ try :
227+ opts , args = getopt .getopt (
228+ sys .argv [1 :],
229+ 'p:rohd' ,
230+ ['parse=' , 'reverse' , 'outputdict' , 'help' , 'dump' ])
231+ except getopt .error , msg :
232+ usage (1 , msg )
233+
234+ for opt , arg in opts :
235+ if opt in ('-h' , '--help' ):
227236 help = 1
228- elif arg in ('-d' , '--dump' ):
237+ elif opt in ('-d' , '--dump' ):
229238 dump = 1
230- elif arg in ('-p' , '--parse' ):
231- parsefile = val
232- elif arg in ('-o' , '--output' ):
239+ elif opt in ('-p' , '--parse' ):
240+ parsefile = arg
241+ elif opt in ('-o' , '--output' ):
233242 normalize = 1
234- elif arg in ('-r' , '--reverse' ):
243+ elif opt in ('-r' , '--reverse' ):
235244 forcerev = 1
236245
237246 if help :
0 commit comments