Created
July 7, 2017 17:29
-
-
Save CharlyJazz/1a8669e2d0484139a43e6904aa58a488 to your computer and use it in GitHub Desktop.
Get country via IP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pycountry | |
| from geoip import geolite2 | |
| #I need test this code | |
| def get_country(request_ip): | |
| math = geolite2.lookup(request_ip) if geolite2.lookup(request_ip) is not None else geolite2.lookup_mine() | |
| this_alpha = math.country | |
| tz = str(math.timezone) | |
| citycapital = tz[tz.index('/')+1:] | |
| for alpha in list(pycountry.countries): | |
| if this_alpha.__contains__(alpha.alpha2): | |
| this_alpha = alpha.name | |
| return str(this_alpha + ' , ' + citycapital) | |
| print (get_country('127.0.0.1')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment