Original: https://redislabs.com/ebook/redis-in-action/part-2-core-concepts-2/ Chapter-5-using-redis-for-application-support/5-3-ip-to-city-and-country-lookup/5-3-1- Loading-the-location-tables
For development data, I downloaded a free IP address from http://dev.maxmind.com/geoip/geolite to the city database. This database contains two important files: the geo-litecity-blocks.csv that contains the IP address range and the city ID range information, and contains the name, region, Countries and some of the information we don't need geolitecity-location.csv
We first construct a lookup table that allows us to use an IP address and convert it to a city ID, and then construct a second lookup table that allows us to use a city ID and convert it to its actual city information (which also includes regional and country information).
This lookup table that uses an IP address and converts it to a city ID will be constructed as a single zset, which uses the city ID as a member and an IP address as a score. To allow us to map from an IP address to a city ID, we convert the point-in-order IP address into an shaping score, using a 32-bit unsigned integer with every eight bits as one byte and the first byte as the highest. The code to do these things look here
#代码https://github.com/huangz1990/riacn-code/blob/master/ch05_listing_source.py#l318
def Ip_to_score (ip_address): = 0 for in Ip_address.split (' . ' ): = score * + int (V, ten) return score
When we have scored, we first add the IP address mapped to the city ID, in order to construct a unique city ID from the general City ID (because multiple IP addresses can be mapped to the same city ID), we will add a _ character followed by a number added to Zset, we can look at the following code:
#https://github.com/huangz1990/riacn-code/blob/master/ch05_listing_source.py#l316
defImport_ips_to_redis (conn, filename): Csv_file= Csv.reader (open (filename,'RB')) forCount, RowinchEnumerate (csv_file):#convert the IP address to a score on demand. START_IP = row[0]ifRowElse "' if 'I' inchstart_ip.lower ():Continue if '.' inchstart_ip:start_ip=Ip_to_score (START_IP)elifstart_ip.isdigit (): Start_ip= Int (START_IP, 10) Else: #Skips the first line of the file and an incorrectly formatted entry. Continue #build a unique city ID. city_id = row[2] +'_'+Str (count)#Add the city ID and its corresponding IP address score to the ordered set. Conn.zadd ('Ip2cityid:', city_id, START_IP)
After executing the function, the effect is as follows:
When the IP address is loaded through Import_ips_to_redis (), we will create a zset to map the city ID to the city information, as shown in the following code. We'll use the JSON format to store city information because our data is a fixed format that doesn't change over time.
#https://github.com/huangz1990/riacn-code/blob/master/ch05_listing_source.py#l354
defImport_cities_to_redis (conn, filename): forRowinchCsv.reader (open (filename,'RB')): ifLen (Row) < 4or notrow[0].isdigit ():ContinueRow= [I.decode ('latin-1') forIinchRow]#prepare the information you want to add to the hash. city_id =Row[0] Country= Row[1] Region= Row[2] City= Row[3] #Add city information to Redis. Conn.hset ('cityid2city:', city_id, Json.dumps ([City, Region, country] )
After you finish executing the code, the effect:
Now that we have all the information in Redis, we can start looking for IP addresses.
Http://dev.maxmind.com/geoip/geolite#sthash.Y0BjekhR.dpufhttp://dev.maxmind.com/geoip/geolite#sthash.Y0BjekhR.dpuf
<redis actual combat >5.3.1 load Address Table