def index( request ):
ip()
return render(request, 'MyWeather_Templates/index.html')
def ip():
headers = {}
headers[
'User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
response = requests.get('http://www.baidu.com/s?wd=ip', headers=headers)
html = response.text
reGET = re.compile('fk="(.*?)"').findall(html)
for i in reGET:
break
ip = i
reader = geoip2.database.Reader('./media/GeoLite2-City.mmdb')
data = reader.city(ip)
city = data.city.names['zh-CN']
print("IP Address: ", ip)
print("国家: ", data.country.names['zh-CN']) # names['zh-CN']转换为中文
print("省份: ", data.subdivisions.most_specific.names['zh-CN'])
print("城市: ", data.city.names['zh-CN'])
print("纬度: ", data.location.latitude) # 位置
print("经度: ", data.location.longitude)
print("时区:",data.location.time_zone)
报错信息:No such file or directory: './GeoLite2-City.mmdb'
我尝试将GeoLite2-City.mmdb放到media外面直接访问,但还是报这个错...这是为啥?
(PS:换成绝对路径就能访问到)