From 0e7b6c9131413a679ee537f95585a6980ad75e74 Mon Sep 17 00:00:00 2001 From: Gregory Soutade Date: Fri, 4 Nov 2022 18:30:28 +0100 Subject: [PATCH] Add Geo IP web service function in misc --- misc/__init__.py | 1 + misc/geoiplookup.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 misc/__init__.py create mode 100644 misc/geoiplookup.py diff --git a/misc/__init__.py b/misc/__init__.py new file mode 100644 index 0000000..792d600 --- /dev/null +++ b/misc/__init__.py @@ -0,0 +1 @@ +# diff --git a/misc/geoiplookup.py b/misc/geoiplookup.py new file mode 100644 index 0000000..bc04f4f --- /dev/null +++ b/misc/geoiplookup.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright Grégory Soutadé 2022 + +# This file is part of iwla + +# iwla is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# iwla is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with iwla. If not, see . +# + +import urllib3 +import json + +def geoiplookup(ip): + http = urllib3.PoolManager() + r = http.request('GET', f'https://api.geoiplookup.net/?query={ip}&json=true') + + if r.status != 200: + raise Exception(r) + + return json.loads(r.data.decode('utf-8'))