加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python查询IP地址归属完整代码

发布时间:2020-12-17 08:20:42 所属栏目:Python 来源:网络整理
导读:本文实例为大家分享了Python查询IP地址归属的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python# -*- coding: utf-8 -*-#查找IP地址归属地#writer by keery_log#Create time:2013-10-30#Last update:2013-10-30#用法: python chk_ip.py www.google.

本文实例为大家分享了Python查询IP地址归属的具体代码,供大家参考,具体内容如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#查找IP地址归属地
#writer by keery_log
#Create time:2013-10-30
#Last update:2013-10-30
#用法: python chk_ip.py www.google.com |python chk_ip.py 8.8.8.8 |python chk_ip.py ip.txt
 
import signal
import urllib
import json
import sys,os,re
import socket
 
if len(sys.argv) <= 1 :
  print "Please input ip address !"
  sys.exit(0)
 
def handler(signum,frame):
  sys.exit(0)
signal.signal(signal.SIGINT,handler)
 
url = "http://ip.taobao.com/service/getIpInfo.php?ip="
 
#查找IP地址
def ip_location(ip):
  data = urllib.urlopen(url + ip).read()
  datadict=json.loads(data)
 
  for oneinfo in datadict:
    if "code" == oneinfo:
      if datadict[oneinfo] == 0:
        return datadict["data"]["country"] + datadict["data"]["region"] + datadict["data"]["city"] + datadict["data"]["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 os.path.isfile(sys.argv[1]): #如果参数是文件,迭代查找
  file_path = sys.argv[1]
  fh = open(file_path,'r')
  for line in fh.readlines():
    if re_ipaddress.match(line):
      city_address = ip_location(line)
      print line.strip() + ":" + city_address
else:
  ip_address = sys.argv[1]
  if re_ipaddress.match(ip_address): #如果参数是单个IP地址
    city_address = ip_location(ip_address)
    print ip_address + ":" + 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() + ":" + city_address

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读