• Python 小练习一


    1.如果同一个ip地址60s之内访问超过200次,那么就把ip加入黑名单

    需求分析:

    1.60s读一次文件

    2.分割,取第一个元素,ip地址

    3.把所有ip加入到一个list里面,如果ip次数超过200次加入黑名单

     

    import time

     

    point = 0 # 文件指针

    while True:

    blk_set = set() # 定义空集合,存放需要加入黑名单的ip

    ips = [] # 存放所有ip地址

    with open('access.log') as f:

    f.seek(point)

    for line in f:

    ip = line.split()[0]

    ips.append(ip)

    if ips.count(ip) > 200:

    blk_set.add(ip)

    for ip in blk_set: # 去重

    print('已把%s加入黑名单' % ip)

    point = f.tell()

    time.sleep(60)

     

     

     

    2.判断是否为小数

    # 小数点的个数,小数点的个数是否等于1

    # 按照小数点分割

     

    def check_float(s):

    s = str(s)

    if s.count('.') == 1:

    s_list = s.split('.')

    # 1.2 [1, 2]

    # -5.3 [-5, 4]

    left = s_list[0]

    right = s_list[1]

    if left.isdigit() and right.isdigit(): # 正小数

    return True

    elif left.startswith('-') and left.count('-') == 1: # 负小数

    if left.split('-')[-1].isdigit() and right.isdigit():

    return True

    return False

     

     

     

    3.对比字典内容

    dict1 = {

    "version": "9.0.0",

    "is_test": True,

    "store": "",

    "urs": "",

    "device": {

    "os": "android",

    "ime1": "99001062198893",

    "device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",

    "mac": "02:00:00:00:00:00",

    "galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",

    "udid": "a34b1f67dd5797df93fdd8b072f1fb8110fd0db6",

    "network_status": "wifi"

    },

    "adunit": {

    "category": "VIDEO",

    "location": "1",

    "app": "7A16FBB6",

    "blacklist": ""

    },

    "ext_param": {

    "is_start": 0,

    "vId": "VW0BRMTEV"

    }

    }

     

    dict2 = {

    "version": "9.0.0",

    "is_test": True,

    "urs": "",

    "store": "",

    "device": {

    "os": "android",

    "imei": "99001062298893",

    "device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",

    "mac": "02:00:00:00:00:00",

    "galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",

    "udid": "a34b1f67dd5797da93fdd8b072f1fb8110fd0db6",

    "network_status": "wifi"

    },

    "adunit": {

    "category": "VIDEO",

    "location": "1",

    "app": "7A16FBB6",

    "blacklist": ""

    }, "ext_param": {

    "is_start": 0,

    "vid": "VW0BRMTEV"

    }

    }

     

     

     

    # 判断两个字典里面不一样的key,value

    # 只需判断d1里面有

    # 循环d1,通过key去d2里面取值

    # 判断通过key取到的类型是什么,如果是字典的话,继续循环

    # 然后再判断key和value

     

    def compare(d1, d2):

    for key in d1:

    value1 = d1.get(key)

    value2 = d2.get(key, '不存在')

    if type(value1) == dict:

    compare(value1, value2)

    else:

    if value1 != value2 and value2 == '不存在':

    print('value不一样的key:%s,value1:%s,value2:%s' % (key, value1, value2))

     

    res = set(d1.keys()).symmetric_difference(set(d2.keys())) # 对称差集,两个字典里面不一样的key

    if res:

    print('key不一样的是', ','.join(res))

     

     

    compare(dict1, dict2)

  • 相关阅读:
    快速击键(MyEclipse编写的QuickHit项目)
    Java语言中的修饰符
    Java中的接口
    建造者模式应用场景
    原型设计模式
    转:fortios 5.4后门植入
    转:json注入
    linux tips
    资料收集
    转:nginx+CGI/FASTCGI
  • 原文地址:https://www.cnblogs.com/louis-w/p/8269231.html
Copyright © 2020-2023  润新知