循环和列表

时间:2017-11-15 20:01:51

标签: python loops structure

尝试创建一个获取IP列表的函数,在点处拆分它们并对每个组执行各种检查以检查它是否有效。如果ip_list中的潜在IP未通过检查,则会将其从列表中删除。这就是它失败的地方:它不会从IP列表中删除测试用例。我只包括第一个检查,因为它是这个(检查潜在的IP是否格式正确:x.x.x.x,即4长)不起作用。如果它不是4长,我希望它从IP列表中删除该失败的地址:

import sys, re, os, time

def verify_ip(ip_list):
    """takes the IP addresses picked up from regex of URL source code and
       verifies they are valid IP addresses."""

    print(ip_list)
    for ip in ip_list:
        octet = ip.split('.')
        print(octet)

        if len(octet) != 4:
            return False
            ip_list.remove(ip)
    print(ip_list)

def main():

    print('\n[-]Verify IP loop TEST ENVIRONMENT [-]')
    time.sleep(1)
    ipv4_list = ['255.255.255.0','192.168.0.1','10.16.231.17',
               '257.10.7.7','126.20.20.4','10.5.5.699',
               '999.999.999.999.999','192.157.8.c']
    verify_ip(ipv4_list)

#  boilerplate
if __name__ == '__main__':
    main()

我收到的输出:

[-]Verify IP loop TEST ENVIRONMENT [-]
['255.255.255.0', '192.168.0.1', '10.16.231.17', '257.10.7.7', 
'126.20.20.4', '10.5.5.699', '999.999.999.999.999', '192.157.8.c']
['255', '255', '255', '0']
['192', '168', '0', '1']
['10', '16', '231', '17']
['257', '10', '7', '7']
['126', '20', '20', '4']
['10', '5', '5', '699']
['999', '999', '999', '999', '999']
>>> 

为什么我的代码没有取出999.999.999.999.999?

0 个答案:

没有答案