Python 生成 ip 地址 - V2EX
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Na1Kh2

Python 生成 ip 地址

  •  1
     
  •   Na1Kh2 Oct 19, 2018 5189 views
    This topic created in 2777 days ago, the information mentioned may be changed or developed.

    from IPy import IP IP('172.10.250-254.250-254') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/IPy.py", line 242, in init raise ValueError("only one '-' allowed in IP Address") ValueError: only one '-' allowed in IP Address</module></stdin>

    IPy 好像不支持这种生成方式,Python 有哪些库可以解决这个问题么?

    13 replies    2018-10-20 11:04:33 +08:00
    stamaimer
        1
    stamaimer  
       Oct 19, 2018 via iPhone
    排个版
    no1xsyzy
        2
    no1xsyzy  
       Oct 19, 2018
    IP 段不连续,不符合规范,唯有用 random 库手撸
    alixali
        3
    alixali  
       Oct 19, 2018
    ValueError: only one '-' allowed in IP Address
    Na1Kh2
        4
    Na1Kh2  
    OP
       Oct 19, 2018
    @stamaimer
    @alixali
    emmmmm 是这样的
    我要按输入的语句生成 ip 地址,只是拿 IPy 举了个栗子
    比如说我输入
    172.10.100-101.1-3
    生成
    172.10.100.1
    172.10.100.2
    172.10.100.3
    172.10.101.1
    172.10.101.2
    172.10.101.3
    ------------------------
    撸了一中午 还没写出来
    @no1xsyzy
    Na1Kh2
        5
    Na1Kh2  
    OP
       Oct 19, 2018
    我现在的想法是:
    把输入的字符串 split('.')分割
    把分割出的 list 循环读取出来
    读取出来的内容用 split('-')分割
    ------
    上面这些都写出来了
    但是组合方法我实在是写不出来了 没有头绪
    no1xsyzy
        6
    no1xsyzy  
       Oct 19, 2018
    不要老想着这是一个 IP 地址,就结束了:
    lambda x: ["%d.%d.%d.%d"%d for d in itertools.product(*[range(m,n+1) for s in x.split(".") for m,n,*_ in [map(int, (s+"-"+s).split("-"))]])]
    no1xsyzy
        7
    no1xsyzy  
       Oct 19, 2018
    #5 看起来需要重练基本功……
    Na1Kh2
        8
    Na1Kh2  
    OP
       Oct 19, 2018
    @no1xsyzy emmm 谢了大佬
    Na1Kh2
        9
    Na1Kh2  
    OP
       Oct 19, 2018
    @no1xsyzy 手动委屈.gif
    hgw514
        10
    hgw514  
       Oct 19, 2018
    https://docs.python.org/3/library/ipaddress.html#ip-network-definitions
    Python3 标准库 ipaddress 可以处理 IP 网段,考虑用 CIDR 格式划分子网?
    pythonnoob
        11
    pythonnoob  
       Oct 20, 2018 via Android
    基本功太差
    pythonnoob
        12
    pythonnoob  
       Oct 20, 2018
    随手写了一个,没考虑效率方面的问题:
    def create_ip_address(one, two, three, four):
    int_list = []
    for i in one, two, three, four:
    if isinstance(i, int):
    i = [i]
    i_len = len(i)
    if i_len == 1:
    int_list.append(i)
    elif i_len == 2:
    int_list.append(range(i[0], i[1]+1))
    else:
    raise ValueError('元组内只能有 1~2 个数字')
    ip_list = []
    for s in int_list:
    s_list = [str(w) for w in s]
    if not ip_list:
    ip_list = s_list
    else:
    ip_list = ['.'.join([o, p]) for p in s_list for o in ip_list]
    return ip_list


    if __name__ == '__main__':
    test_create = create_ip_address(1, (3, 50), (5, 6), (7, 8))
    print(len(test_create), test_create)
    pythonnoob
        13
    pythonnoob  
       Oct 20, 2018
    图: https://s1.ax1x.com/2018/10/20/i01I00.jpg
    注:也没考虑捣乱的情况
    About     Help     Advertise     Blog     API     FAQ     Solana     960 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 38ms UTC 19:47 PVG 03:47 LAX 12:47 JFK 15:47
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86