Try and handle python exceptions more gracefully.

This commit is contained in:
Manuel Friedli 2020-11-24 22:04:38 +01:00
parent a1d5821cc7
commit 70d41673f7

View file

@ -63,16 +63,16 @@ def parse_command_line(argv):
try:
opts, args = getopt.getopt(argv, "f:")
except getopt.GetoptError as e:
raise LookupException("Error parsing command line") from e
raise LookupException("Error parsing command line. Usage: geoip-lookup.py -f /path/to/geoip2-database.mmdb 192.168.1.1") from e
for opt, arg in opts:
if opt == "-f":
dbfile = arg
else:
raise LookupException("Unknown command line argument")
raise LookupException("Unknown command line argument. Usage: geoip-lookup.py -f /path/to/geoip2-database.mmdb 192.168.1.1")
if len(args) == 0:
raise LookupException("No address given on command line")
raise LookupException("No address given on command line. Usage: geoip-lookup.py -f /path/to/geoip2-database.mmdb 192.168.1.1")
return dbfile, args[0]
@ -83,9 +83,13 @@ def main(argv):
:param argv: Format: "-f /path/to/database.mmdb ip.v4.add.ress"
:return:
"""
try
(dbfile, ipaddress) = parse_command_line(argv)
code = get_county_code(ipaddress, dbfile)
print(code)
except LookupException as e:
print(e, file=sys.stderr)
print("Unknown")
if __name__ == '__main__':