python获取ip和具体地址

1:获取ip,在django的request中META里面:

python;toolbar:false">if request.META.has_key('HTTP_X_FORWARDED_FOR'):  

    ip =  request.META['HTTP_X_FORWARDED_FOR']  

else:  

    ip = request.META['REMOTE_ADDR']

2:获取具体ip所在地址:

url = "http://ip.taobao.com/service/getIpInfo.php?ip="

data = json.loads(urllib.urlopen(url + ip).read())

for oneinfo in datadict:

    if "code" == oneinfo:

        if datadict[oneinfo] == 0:

            return datadict["data"]["country"] + datadict["data"]["region"] + datadict["data"]["city"]

 

以上是 python获取ip和具体地址 的全部内容, 来源链接: www.h5w3.com/116188.html

回到顶部