如何检测访问您网站的用户所在的国家和城市

如何检测访问您网站的用户所在的国家和城市

本文介绍了如何检测访问您网站的用户所在的国家和城市?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何检测访问您网站的用户的来源国?

How can you detect the country of origin of the users accessing your site?

我在我的网站上使用 Google Analytics,可以看到我有来自世界不同地区的用户.

I'm using Google Analytics on my site and can see that I have users coming from different regions of the world.

但在我的应用程序中,我想根据国家/地区甚至城市提供一些额外的自定义.

But within my application I would like to provide some additional customization based on the country and perhaps the city.

是否可以从浏览器中检测到此信息?这是一个 Python 网络应用程序.

Is it possible to detect this infomation from the browser? This is a Python web application.

推荐答案

Grab and gunzip http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz,安装 GeoIP-Python 包(python-geoip 包如果你在 Debian 或 Ubuntu,否则安装 http://geolite.maxmind.com/download/geoip/api/python/GeoIP-Python-1.2.4.tar.gz),然后执行:

Grab and gunzip http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz, install the GeoIP-Python package (python-geoip package if you're in Debian or Ubuntu, otherwise install http://geolite.maxmind.com/download/geoip/api/python/GeoIP-Python-1.2.4.tar.gz), and do:

import GeoIP
gi = GeoIP.open("GeoLiteCity.dat", GeoIP.GEOIP_INDEX_CACHE | GeoIP.GEOIP_CHECK_CACHE)
print gi.record_by_name("74.125.67.100") # a www.google.com IP

{'city': 'Mountain View', 'region_name': 'California', 'region': 'CA', 'area_code': 650, 'time_zone': 'America/Los_Angeles', 'longitude':-122.05740356445312,'country_code3':'USA','纬度':37.419200897216797,'postal_code':'94043','dma_code':807,'Unitname:'country'country

数据库是免费的 (http://geolite.maxmind.com/download/geoip/database/LICENSE.txt).他们也卖.我认为这只会让您更频繁地更新.

The database is free (http://geolite.maxmind.com/download/geoip/database/LICENSE.txt). They do sell it, too; I think that just gets you more frequent updates.

这篇关于如何检测访问您网站的用户所在的国家和城市?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:54