Python3 - Batch query IP attribution using the PureIP database


While I was lazily writing a website log analysis article, I found the need to use the Pure IP database to batch query the geolocation of IP addresses.

So, in the midst of my laziness, I decided to write this article on batch querying IP geolocation using Python. It's a perfect example of laziness at its finest.

This article falls under the categories of both Python and Tool Sharing.

Introduction to the Pure IP Database#

The Pure IP database parses the qqwry.dat library file. The QQWry IP database collects the latest and most accurate IP address data from ISPs such as China Telecom, China Mobile, China Unicom, Great Wall Broadband, and Juyou Broadband. The IP database is updated every 5 days and requires regular updates for the latest IP database.

The software looks like this:


Since it is frequently updated, I found a GitHub project that scans and synchronizes the latest qqwry.dat file every day.

To obtain the latest qqwry.dat, click here to download (requires a VPN).

You can also download this latest version and rename it to replace the original, so you don't have to keep downloading the Pure IP database installation package.

Batch Querying with Python#

For a comprehensive library, please refer to the first reference article at the end of this document, qqwry-python3.

The tool written by this author has been uploaded to pypi, so you can simply use pip install qqwry-py3 to install it.

~~ However, it seems difficult to write, so let's just use the mature qqwry library for now. If I have any other ideas later, I will update this article ~~.

~~ The final version of the code is as follows: ~~

** It seems difficult to write, so let's just use the mature qqwry library. I probably won't write it anymore. **


The code for batch querying IP addresses from a text file and outputting the results is as follows:

# -*- coding: utf-8 -*-
@author: soapffz
@fucntion: Batch query the geolocation of IP addresses in the txt file using the Pure IP database qqwry.dat
@time: 2019-06-28

from qqwry import QQwry
from IPy import IP

def batch_query_and_print():
    q = QQwry()
    with open('ip.txt') as f:
        ip_list =
        for read_content in ip_list:
                print("There are IP addresses that do not comply with the format. Please check and run again.")
    address_list = [q.lookup(ip) for ip in ip_list]
    for i, j in zip(ip_list, address_list):
        query_results = i+" "+j[0]+" "+j[1]
        with open("query_results.txt", 'a', encoding='utf-8') as f:

if __name__ == "__main__":

Install the qqwry and IPy libraries using pip install.

Place the IP addresses you want to query in ip.txt and run the script in the same directory as qqwry.dat and the .py file.

The result is as follows:


Reference article:

Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.