前端页面-由域名获得ip地址,取得地图

虽然是个可要可不要的功能,但都写了,就加上

index.html

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=toGFnmI4HUgy2EpwyCpMGZvS7GqePNnA"></script>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
//地图
function city_map(city) {
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(city,12);
}
function requests(url){
api_url="get_city_from_domain"
$.ajax({
method:'GET',
data:{"url":url},
url:api_url,
success:function(data){
//alert(data)
city_map(data)
}
});

views.py视图中处理:

def get_city_from_domain(request):
url=request.GET.get("url").lstrip("http://").lstrip("https://")
ip = socket.gethostbyname(url)
url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip
s = requests.get(url=url)
print(s.text)
city = json.loads(s.text)["data"]["city"]
return HttpResponse(city)


2019.4.15

标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注