Skip to main content

Easily get the country code from an ip address.

Project description

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

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 database
  • download_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

location_ipfire_db_reader-1.0.5.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

location_ipfire_db_reader-1.0.5-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file location_ipfire_db_reader-1.0.5.tar.gz.

File metadata

File hashes

Hashes for location_ipfire_db_reader-1.0.5.tar.gz
Algorithm Hash digest
SHA256 032e2f92cb3ea3e534849a3492a26d67316f7c15603783c7d9334c4a4264a37f
MD5 cfe9a97d2811c8850273a33b0214dcd5
BLAKE2b-256 5c65385b1af0fc9c0ad7f029571442840ea23c5afb786fd158c128077a1935ac

See more details on using hashes here.

File details

Details for the file location_ipfire_db_reader-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for location_ipfire_db_reader-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c3353a9af12dc608672fc348cb64dbe17fa47639de64b3891be72c81d1210041
MD5 121738dc2c0b1295705f97c516c96b8f
BLAKE2b-256 42908b6b6e69f060eabefabb4ff9b532b206cff4cd53b9bd1c28561dd3f032ce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page