Python API-- query ip-based GeoIP2 module location

Refer to the official documentation https://s0geoip20readthedocs0io.icopy.site/en/latest/

First, the preparatory work

1. Install geoip2Module

$ pip install geoip2

2.下载GeoLite2 free database   https://www.maxmind.com/en/accounts/146736/geoip/downloads

.Mmdb database to obtain city suffix

 

 Second, the code

geoip2.database Import 

Reader = geoip2.database.Reader ( ' C: /Users/A/Downloads/GeoLite2-City_20191224/GeoLite2-City_20191224/GeoLite2-City.mmdb ' ) # City route database
 
ip = the INPUT ( ' Enter need ip address query: ' ) 

Response = reader.city (ip) 

Print ( ' country name ' + response.country.names [ ' ZH-the CN ' ]) 
Print ( ' province name ' + response.subdivisions.most_specific. names [ ' ZH-the CN ' ]) 
Print ( ' city name 'Response.city.names + [ ' ZH-the CN ' ]) 
Print (
' latitude ' + STR (response.location.latitude)) Print ( ' longitude ' + STR (response.location.longitude))

Enter the ip address lookup results

Guess you like

Origin www.cnblogs.com/dummersoul/p/12120289.html
Recommended