CSV

From XPUB & Lens-Based wiki
Revision as of 16:04, 9 February 2015 by Michael Murtaugh (talk | contribs) (Created page with "Reading a geoip database, nb uses pip install ipaddress <source lang="python"> import csv, sys, ipaddress, sys csvfilename = sys.argv[1] # "IP2LOCATION-LITE-DB1.CSV" input_...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Reading a geoip database, nb uses

pip install ipaddress

import csv, sys, ipaddress, sys

csvfilename = sys.argv[1] # "IP2LOCATION-LITE-DB1.CSV"
input_raw = raw_input("Give me an IP?")
input_raw = ".".join(input_raw.split(".")[:3] + ["0"])
# print input_raw
# sys.exit(0)
input_num =int(ipaddress.IPv4Address(unicode(input_raw)))

print "your ip address", input_raw, "is the same as", input_num

csvfile = open(csvfilename)
last_row = None
for row in csv.reader(csvfile):
    from_ip, to_ip, cc, description = row[0], row[1], row[2], row[3]
    from_ip = int(from_ip)
    to_ip = int(from_ip)


    if (last_row):
        if input_num >= last_row[0] and input_num <= to_ip:
            print last_row
            #print "***" , from_ip, to_ip, cc, description

    last_row = (from_ip, to_ip, cc, description)

    # ipaddress.IPv4Address(from_ip)
    # ipaddress.IPv4Address(int(to_ip))
    # print from_ip, to_ip, cc