python for android : 查找IP地址归属地

chk_ip.py

感谢原作者: keery_log

# -*- coding: utf-8 -*-
import android
droid = android.Android()
ip_address = droid.dialogGetInput(u‘查找IP地址归属地‘, u‘请输入IP or 域名:‘).result
if ip_address is None:
    print ‘ Error: ip is None ‘
    sys.exit(4)

import urllib
import json
import re
import socket
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8‘)
url = "http://ip.taobao.com/service/getIpInfo.php?ip="
print ip_address
# 查找IP地址
def ip_location(ip):
    handler = urllib.urlopen(url + ip)
    data = handler.read()
    handler.close()
    datadict=json.loads(data)
    for oneinfo in datadict:
        if "code" == oneinfo:
            if datadict[oneinfo] == 0:
                d = datadict["data"]
                return d["country"]+d["region"]+d["city"]+d["isp"]

# 定义IP与域名正则
re_ipaddress = re.compile(r‘^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$‘)
re_domain = re.compile(r‘[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?‘)

if re_ipaddress.match(ip_address):  # 如果参数是单个IP地址
    city_address = ip_location(ip_address)
    print ip_address + u": " + city_address
elif(re_domain.match(ip_address)):  # 如果参数是域名
    result = socket.getaddrinfo(ip_address, None)
    ip_address = result[0][4][0]
    city_address = ip_location(ip_address)
    print ip_address.strip() + u": " + city_address

python for android : 查找IP地址归属地,布布扣,bubuko.com

python for android : 查找IP地址归属地

上一篇:Android学习之绘图入门


下一篇:第十篇、微信小程序-view组件