Easily get the country code from an ip address.
Project description
from _testcapi import raise_exceptionfrom location_ipfire_db_reader import LocationDatabase
location_ipfire_db_reader
Introduction
I was looking for a geo-ip library where I can easily convert IPs into countries. And this preferably in Python and preferably offline.
Couldn't find any, but ipfires location
utility came very close. Sadly it was coded in C.
That's why I re-interpreted the location databases structure into Python and made a tiny wrapper around it for easy access.
Installation:
pip install -U location-ipfire-db-reader
Main usage:
from location_ipfire_db_reader import LocationDatabase
# This call will _download the location database into the provided file.
# It will not re-_download it if there are no updates.
db = LocationDatabase('location.db')
print(db.find_country('8.8.8.8')) # US
This library should work for both IP4 & IP6.
Get more information
What if you wanted to get (much) more information? Like the continent, or the provider? These are all things contained in the ipfires database.
from location_ipfire_db_reader import LocationDatabase
db = LocationDatabase('location.db')
ip_info = db["8.8.8.8"]
print(f"""
ip: {ip_info.ip}
subnet_mask: {ip_info.subnet_mask}
network_address: {ip_info.network_address}
ip_with_cidr: {ip_info.ip_with_cidr}
asn: {ip_info.asn}
asn_name: {ip_info.asn_name}
country_code: {ip_info.country_code}
country_name: {ip_info.country_name}
country_continent: {ip_info.country_continent}
is_anonymous_proxy: {ip_info.is_anonymous_proxy}
is_satellite_provider: {ip_info.is_satellite_provider}
is_anycast: {ip_info.is_anycast}
is_drop: {ip_info.is_drop}
""")
This will output:
ip: 8.8.8.8
subnet_mask: 24
network_address: 8.8.8.0
ip_with_cidr: 8.8.8.0/24
asn: 15169
asn_name: GOOGLE
country_code: US
country_name: United States of America
country_continent: NA
is_anonymous_proxy: False
is_satellite_provider: False
is_anycast: True
is_drop: False
Exceptions
All exceptions within this package will inherit from LocationIPFireDBReaderException
. So if you want a blanket-capture-all. That's what you'll need.
However, these are more fine-tuned versions:
UnknownASNName
: will be raised when an ASN is found, but there is no known name for it.IPAddressError
: will be raised when an IP lookup fails. This happens with reserved IPs so far.
What if you don't want exceptions?
If you don't like to handle exceptions, you can always initialize your LocationDatabase
like this:
db = LocationDatabase(raise_exceptions=False)
Now if for example the AS isn't known, it will output 0
for the asn, and ""
for the AS name. Instead of raising an exception.
Developers information
(or more accurately named: information for myself at a future point in time 😎)
database.py
: The wrapper, ie: consumer-facing code.database_reader.py
: The wrapper around the filehandling and reading and stuff.decompress_db.py
contains just the code to facilitate extraction of the databasedownload_db.py
: download (or update) once a day the newest ipfire location database.interpret_location_db.py
: contains the low-level interpretation of the database file.ip_information.py
: contains the class where all information can be retrieved from ipfires database.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file location_ipfire_db_reader-1.0.6.tar.gz
.
File metadata
- Download URL: location_ipfire_db_reader-1.0.6.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.9.18 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21ae3db93a4a1e8c917b75baf52703164cfa8b5e5d99ef03f340473eabce4827 |
|
MD5 | b3057e6ec5c881107f56adf2716d6e33 |
|
BLAKE2b-256 | 5503ae6b870f001f33115701e8e7fd08cb406fae599932f9a8b634ab284e98b6 |
File details
Details for the file location_ipfire_db_reader-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: location_ipfire_db_reader-1.0.6-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.9.18 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcf2ec95ef93574b7a36913055696f1dc2bd0fde2841f1e5d520802deb4af67e |
|
MD5 | 0e0f2c82b8fa0e60257639e4fe0440b1 |
|
BLAKE2b-256 | 5e266f5bfae6f71a911b5c212b3c0be6a670d74b5a0db3633b78d16e18b8bbc1 |