geoip2 3.0.0
Project description
Description
This package provides an API for the GeoIP2 web services and databases. The API also works with MaxMind’s free GeoLite2 databases.
Installation
To install the geoip2 module, type:
$ pip install geoip2
If you are not able to use pip, you may also use easy_install from the source directory:
$ easy_install .
Database Reader Extension
If you wish to use the C extension for the database reader, you must first install the libmaxminddb C API. Please see the instructions distributed with it.
IP Geolocation Usage
IP geolocation is inherently imprecise. Locations are often near the center of the population. Any location provided by a GeoIP2 database or web service should not be used to identify a particular address or household.
Usage
To use this API, you first create either a web service object with your MaxMind account_id and license_key or a database reader object with the path to your database file. After doing this, you may call the method corresponding to request type (e.g., city or country), passing it the IP address you want to look up.
If the request succeeds, the method call will return a model class for the end point you called. This model in turn contains multiple record classes, each of which represents part of the data returned by the web service.
If the request fails, the client class throws an exception.
Web Service Example
import geoip2.webservice
This creates a Client object that can be reused across requests.
Replace "42" with your account ID and "license_key" with your license
key.
client = geoip2.webservice.Client(42, 'license_key')
Replace "insights" with the method corresponding to the web service
that you are using, e.g., "country", "city".
response = client.insights('128.101.101.101')
response.country.iso_code
'US'
response.country.name
'United States'
response.country.names['zh-CN']
u'美国'
response.subdivisions.most_specific.name
'Minnesota'
response.subdivisions.most_specific.iso_code
'MN'
response.city.name
'Minneapolis'
response.postal.code
'55455'
response.location.latitude
44.9733
response.location.longitude
-93.2323
response.traits.network
IPv4Network('128.101.101.101/32')
Web Service Client Exceptions
For details on the possible errors returned by the web service itself, see http://dev.maxmind.com/geoip/geoip2/w... for the GeoIP2 Precision web service docs.
If the web service returns an explicit error document, this is thrown as a AddressNotFoundError, AuthenticationError, InvalidRequestError, or OutOfQueriesError as appropriate. These all subclass GeoIP2Error.
If some other sort of error occurs, this is thrown as an HTTPError. This is thrown when some sort of unanticipated error occurs, such as the web service returning a 500 or an invalid error document. If the web service returns any status code besides 200, 4xx, or 5xx, this also becomes an HTTPError.
Finally, if the web service returns a 200 but the body is invalid, the client throws a GeoIP2Error.
Database Example
City Database
import geoip2.database
This creates a Reader object. You should use the same object
across multiple requests as creation of it is expensive.
reader = geoip2.database.Reader('/path/to/GeoLite2-City.mmdb')
Replace "city" with the method corresponding to the database
that you are using, e.g., "country".
response = reader.city('128.101.101.101')
response.country.iso_code
'US'
response.country.name
'United States'
response.country.names['zh-CN']
u'美国'
response.subdivisions.most_specific.name
'Minnesota'
response.subdivisions.most_specific.iso_code
'MN'
response.city.name
'Minneapolis'
response.postal.code
'55455'
response.location.latitude
44.9733
response.location.longitude
-93.2323
response.traits.network
IPv4Network('128.101.101.0/24')
reader.close()
geo.py
import geoip2.database
reader = geoip2.database.Reader(‘./GeoLite2-City_20190115/GeoLite2-City.mmdb’)
response = reader.city(‘123.201.171.134’)
print(response.country.iso_code)
print(response.country.name)
print(response.postal.code)
print(response.subdivisions.most_specific.name)
print(response.city.name)
print(response.location.latitude)
print(response.location.longitude)
That’s it! Hope it helps to you.
Watch video How to locate an IP address using python ? | Track the exact location with IP address using python online without registration, duration hours minute second in high quality. This video was added by user Engineers Revolution 12 April 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,626 once and liked it 27 people.