zoukankan      html  css  js  c++  java
  • 批量解析域名为IP地址的python脚本

    脚本如下:

    #!/usr/bin/env python
    #coding:utf-8
    
    
    import os,sys
    from socket import gethostbyname
    DOMAIN= "G:/PycharmProject/fullstack2/week1/domain.txt"
    
    def main():
        #domain.txt里面存储的是需要批量解析的域名列表,一行一个
        with open(DOMAIN,'r') as f:
            for line in f.readlines():
                try:
                     host = gethostbyname(line.strip('
    '))
                except Exception as e:
                    print e
                else:
                    #result.txt里面存储的是批量解析后的结果,不用提前创建
                    with open('result.txt','a+') as r:
                        r.write(line.strip('
    ') + ' ')
                        r.write(host + '
    ')
    if __name__ == '__main__':
        main()

    这个脚本不完善,因为IP和域名绑定了,但是IP ping不通或者机器关机会出现如下错误

    [Errno 11001] getaddrinfo failed
    [Errno 11001] getaddrinfo failed
    [Errno 11001] getaddrinfo failed
    [Errno 11001] getaddrinfo failed

    而且无法确定哪个域名没有和IP绑定

    完善脚本:原创

    #!/usr/bin/env python
    #coding:utf-8
    
    
    from socket import gethostbyname
    DOMAIN= "G:/PycharmProject/fullstack2/week1/e.txt"
    
    with open(DOMAIN,'r') as f:
    
         for line in f.readlines():
            try:
                host = gethostbyname(line.strip('
    '))  #域名反解析得到的IP
            except Exception as e:
                with open('error.txt','a+') as ERR:  #error.txt为没有IP绑定的域名
                    ERR.write(line.strip()+ '
    ')
            else:
                with open('result.txt','a+') as r: #result.txt里面存储的是批量解析后的结果
                    r.write(line.strip('
    ') + ' ')   #显示有ip绑定的域名,用空格隔开
                    r.write(host + '
    ')

    参考:

    http://www.tiaozhanziwo.com/archives/166.html

  • 相关阅读:
    高斯消元(学习笔记)
    离散化(学习笔记)
    并查集(学习笔记)
    模板---负环(学习笔记)
    差分数组 前缀和(学习笔记)
    manacher马拉车算法(学习笔记)
    Java 常用对象-Object类
    Java 值传递和引用传递
    Java 集合-Set接口和三个子类实现
    Java 集合-Arrays工具类的介绍
  • 原文地址:https://www.cnblogs.com/zhongguiyao/p/9004807.html
Copyright © 2011-2022 走看看