Add Geo IP web service function in misc

This commit is contained in:
Gregory Soutade 2022-11-04 18:30:28 +01:00
parent b92a017dba
commit 0e7b6c9131
2 changed files with 33 additions and 0 deletions

1
misc/__init__.py Normal file
View File

@ -0,0 +1 @@
#

32
misc/geoiplookup.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
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'))