判断一个ip是否在设置的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
loading
0.06D

判断一个ip是否在设置的ip段怎么写

  •  
  •   loading Feb 11, 2014 via iPhone 6434 views
    This topic created in 4472 days ago, the information mentioned may be changed or developed.
    目前写一个应用要针对ip进行简单限制,现在将配置文件写在xx.conf中用ConfigParser读取,现在我是这样写的:

    allowIp=['127.0.0.1','ip2','ip2']

    使用
    if getIp() not in allowIp:
    pass

    这样比较灵活,但如果一大段地址怎么写呢?如加上掩码的写法要怎么判断在不在里面呢?
    192.168.1.1/8
    11 replies    1970-01-01 08:00:00 +08:00
    jasontse
        1
    jasontse  
       Feb 11, 2014 via iPad   1
    ip2long
    binux
        2
    binux  
       Feb 11, 2014
    http://zh.wikipedia.org/wiki/IPv4#.E5.9C.B0.E5.9D.80.E6.A0.BC.E5.BC.8F

    /8表示最后8未可以是任意值

    然后与一下就好了
    dorentus
        3
    dorentus  
       Feb 11, 2014
    @binux
    x.x.x.x/8 是表示除了前八位(二进制形式),后面的所有位可以是任意值。
    dorentus
        4
    dorentus  
       Feb 11, 2014
    @dorentus
    PS: x.x.x.x/8 里面的 8 其实就是子网掩码啦,/8 等同于 /255.0.0.0。
    Ever
        5
    Ever  
       Feb 11, 2014   1
    ipaddr和netaddr两个库都能很好的处理这个.
    loading
        6
    loading  
    OP
       Feb 11, 2014 via iPhone
    @jasontse
    @Ever 感谢


    @binux
    @dorentus
    @dorentus 这个怎么会不知道呢…
    dorentus
        7
    dorentus  
       Feb 11, 2014
    @loading
    嗯,我跑题了……

    不过不用别的工具的话,binux 的思路其实是可行的。
    loading
        8
    loading  
    OP
       Feb 11, 2014 via iPhone
    @binux
    @dorentus 嗯,就是太笨,太懒…
    stillness
        9
    stillness  
       Feb 11, 2014
    ipaddr 有个 Contains 方法
    magine
        10
    magine  
       Feb 12, 2014
    python 有个IPy包
    Mutoo
        11
    Mutoo  
       Feb 12, 2014
    ip/x 是末位子网掩码,不能满足你的需求的话,可以用完整的子网掩码,一般都可以达到目的。
    About     Help     Advertise     Blog     API     FAQ     Solana     1082 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 60ms UTC 22:52 PVG 06:52 LAX 15:52 JFK 18:52
    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