Python实现通过解析域名获取ip地址的方法分析

本文实例讲述了Python实现通过解析域名获取ip地址的方法。分享给大家供大家参考,具体如下:

从网上查找的一些资料,特此做个笔记

案例1:

def getIP(domain):
  myaddr = socket.getaddrinfo(domain, 'http')
  print(myaddr[0][4][0])

执行函数

getIP("www.google.com")

案例2:

def get_ip_list(domain): # 获取域名解析出的IP列表
  ip_list = []
  try:
    addrs = socket.getaddrinfo(domain, None)
    for item in addrs:
      if item[4][0] not in ip_list:
        ip_list.append(item[4][0])
  except Exception as e:
    # print(str(e))
    pass
  return ip_list

PS:这里再为大家推荐一款功能相似的在线工具供大家参考:

IP地址归属地在线查询工具:
http://tools.laike.net/aideddesign/ipcha

另外,本站在线工具小程序上也有一款功能更加强大的IP地址解析工具,感兴趣的朋友可以扫描如下小程序码查看:

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python数学运算技巧总结》

希望本文所述对大家Python程序设计有所帮助。