zoukankan      html  css  js  c++  java
  • python:生成指定范围内的所有ip

    生成指定范围内的所有ip

    #codint:utf8
    def get_side(pos, now):
        global begin, end
        global first, last                             #
        find_side = lambda begin, end, pos, fisrt, last, now: 
            (pos == first[now] and begin[now + 1] or 1, pos == last[now] and end[now + 1] or 255)
        first[now + 1], last[now + 1] = find_side(begin, end, pos, first, last, now)
        # print first
        return range(first[now + 1], last[now + 1] + 1)
     
     
    def gen_ip(ip):     
        global begin, end
        global first, last
        sides = ip.split('-')
        begin = map(lambda x: int(x), sides[0].split('.'))
        end = map(lambda x: int(x), sides[1].split('.'))
        first[0] = begin[0]
        last[0] = end[0]
        ip_str = []   
     
        for pos_one in range(begin[0], end[0] + 1):
            for pos_two in get_side(pos_one, 0):
                for pos_three in get_side(pos_two, 1):
                    for pos_four in get_side(pos_three, 2):
                        str = "%d.%d.%d.%d" % (pos_one, pos_two, pos_three, pos_four)
                        ip_str.append(str)
     
        return ip_str
    if __name__ == '__main__':
        global begin, end
        global first, last, ips
        first = ["", "", "", ""]
        last = ["", "", "", ""]
        ips = gen_ip("120.76.0.0-120.79.255.255")
        print ips
    View Code

    生成指定范围内的IP段

    #!/usr/local/bin/python
    #-*- coding: UTF-8 -*-
    #############################################################
    # python 生成IP段
    #############################################################
    if __name__ == '__main__':
      result=file('test.txt','w')
      for IP1 in range(254,0,-1):
        for IP2 in range(254,0,-1):
          #for IP3 in range(254,0,-1):
            #for IP4 in range(254,0,-1):
              #print IP1,IP2
            abc= '%s.%s.%s-%s.%s.%s
    ' % (IP1,IP2,"1.1",
                        IP1,IP2,"255.255")
            result.write(abc)
      result.close()
    View Code
    每天努力一点,每天学习一点。 Keep Moving...
  • 相关阅读:
    JDBC提高mysql入库的效率!
    Java域名解析,类似nslookup
    request.getReader()的怪异事件
    程序一部署上就占了tomcat的2G内存
    nginx简介及简单使用
    ubuntu安装hadoop(伪分布)
    .Net Famework 和 Library的源码下载
    将switch case转为条件驱动
    Asp.net 和 Comet 开发入门, 第一个例子
    HTML, CSS和Javascript调试入门
  • 原文地址:https://www.cnblogs.com/channy14/p/14777608.html
Copyright © 2011-2022 走看看