{"library":"mmdb-writer","title":"mmdb-writer Library","description":"mmdb-writer is a Python library for creating MaxMind DB (.mmdb) format files, which can be efficiently read by MaxMind's official client libraries in various programming languages. It allows users to associate custom data with IP address ranges (IPv4 and IPv6). The current version is 0.2.6, with updates released periodically, though not on a fixed schedule.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install mmdb-writer"],"cli":{"name":"mmdb-writer","version":"sh: 1: mmdb-writer: not found"}},"imports":["from mmdb_writer import MMDBWriter"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom mmdb_writer import MMDBWriter\nfrom netaddr import IPNetwork # Optional, strings also accepted by insert_network\n\n# Define the output database file\noutput_file = 'my_ip_database.mmdb'\n\n# Initialize the writer\n# ip_version: 4 for IPv4 only, 6 for mixed IPv4/IPv6.\n# database_type: An arbitrary string to identify your database.\n# record_size: Affects max data size per IP entry (e.g., 24 for ~1.5KB, 32 for ~6KB).\nwriter = MMDBWriter(\n    ip_version=6,\n    database_type='Custom-Geo-Database',\n    languages=['en', 'es'],\n    description={'en': 'My custom IP lookup database', 'es': 'Mi base de datos IP personalizada'},\n    record_size=24, # Recommended for many common use cases\n    store_ipv4_in_ipv6_format=True # Recommended when ip_version is 6\n)\n\n# Insert individual networks with associated data\n# Data can be a dictionary, list, string, int, float, or boolean.\nwriter.insert_network('1.1.1.0/24', {'country': 'US', 'city': 'Exampleville', 'latitude': 34.05, 'longitude': -118.25})\nwriter.insert_network('2.2.2.0/24', {'country': 'CA', 'isp': 'InternetCorp'})\n\n# Insert a list of networks with the same data\n# Can use strings directly or netaddr.IPNetwork objects.\nnetworks_for_region = [\n    '3.3.3.0/28',\n    IPNetwork('3.3.3.16/28') # Both strings and IPNetwork objects are fine here\n]\nwriter.insert_network_list(networks_for_region, {'country': 'UK', 'continent': 'Europe', 'timezone': 'Europe/London'})\n\n# Insert an IPv6 network\nwriter.insert_network('2001:db8::/32', {'country': 'DE', 'organization': 'TechSolutions GmbH'})\n\n# Close the writer to finalize and write the database file\nwriter.close(output_file)\n\nprint(f\"MMDB database '{output_file}' created successfully.\")\n\n# Clean up the created file (uncomment if you want to remove it automatically)\n# os.remove(output_file)","lang":"python","description":"This quickstart demonstrates how to initialize `MMDBWriter`, insert IPv4 and IPv6 networks with various data types, and finalize the database creation. It highlights the use of `insert_network` for single entries and `insert_network_list` for multiple, sharing data. Key parameters like `ip_version`, `database_type`, and `record_size` are explained.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}