Skip to content

Instantly share code, notes, and snippets.

@CharlyJazz
Created July 7, 2017 17:29
Show Gist options
  • Select an option

  • Save CharlyJazz/1a8669e2d0484139a43e6904aa58a488 to your computer and use it in GitHub Desktop.

Select an option

Save CharlyJazz/1a8669e2d0484139a43e6904aa58a488 to your computer and use it in GitHub Desktop.
Get country via IP
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