F
F
fringer2022-02-03 23:41:28
Django
fringer, 2022-02-03 23:41:28

How to determine the distance between two ip using Django?

I can't figure out how best to implement this mechanism.
Just started learning DRF.
At first I thought to make a locator through Yandex, but so far I have not figured out how to send a request there and receive from them. JSON request format.

Maybe there is a good way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2022-02-04
@bacon

And how will Django and DRF help you with this? No way, they are needed if you want to wrap your Wishlist in a service and that's it.
Now to the Wishlist itself, what is the "distance between two ip"? Who needs it and why?

F
fringer, 2022-02-04
@fringer

Found a solution through an external API
Here is a way to get coordinates, the rest is trifles.
Suddenly someone needs it, like me.

import urllib, json

PRIVATE_IPS_PREFIX = ('10.', '172.', '192.',)


def get_client_ip(request):
    remote_address = request.META.get('REMOTE_ADDR')
    ip = remote_address
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        proxies = x_forwarded_for.split(',')
        while (len(proxies) > 0 and
               proxies[0].startswith(PRIVATE_IPS_PREFIX)):
            proxies.pop(0)
        if len(proxies) > 0:
            ip = proxies[0]
    return ip


def get_res(request):
    client_ip= get_client_ip(request)
    if client_ip is None:
        client_ip = "0.0.0.0"
    ip_address = '106.220.90.88'
    try:
        url = 'https://api.ipfind.com/?ip=' + client_ip
        response = urllib.request.urlopen(url)
        data1 = json.loads(response.read())
        longitude = data1["longitude"]
        latitude = data1["latitude"]
    except:
        url = 'https://api.ipfind.com/?ip=' + ip_address
        response = urllib.request.urlopen(url)
        data1 = json.loads(response.read())
        longitude = data1["longitude"]
        latitude = data1["latitude"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question