I have a CSV file, where IP ranges are stored along with their landcode:
"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
This can be read like this:
range_start, range_stop, ignored, ignored, country_code, country_name
When the user asks for a particular IP address, I want to return him the country code corresponding to this IP. Here for example, 1.0.9.10
would return CN
for China, because it is between 1.0.8.0
and 1.0.15.255.
I don't know how to deal with that. Here is what I've done so far, but I doubt I'm in the right direction:
import csv
with open("GeoIPCountryWhois.csv") as csvfile:
readCSV = csv.reader(csvfile, delimiter = ",")
IP_LOWs = []
IP_HIGHs = []
Land_CODEs = []
Lands = []
for row in readCSV:
IP_LOW = row[0]
IP_HIGH = row[1]
Land_CODE = row[4]
Land = row[5]
IP_LOWs.append(IP_LOW)
IP_HIGHs.append(IP_HIGH)
Land_CODEs.append(Land_CODE)
Lands.append(Land)
whatIP = input("What IP should be looked up? ")
codedex = IP_LOWs.index(whatIP)
landdex = Lands.index(whatIP)
IP_to_Code = Land_CODE[codedex]
IP_to_land = Land[landdex]
print(IP_to_Code)
print(IP_to_land)