• Python3 常用模块2


    time 模块

    time 模块提供了三种不同类型的时间, 三种不同类型的时间可以相互转换

    时间戳形式

    print(time.time())  # 1569668018.1848686
    

    格式化时间

    print(time.strftime('%Y-%m-%d %X'))  # 2019-01-28 18:55:25
    

    结构化时间

    print(time.localtime())  # time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=18, tm_min=57, tm_sec=9, tm_wday=5, tm_yday=271, tm_isdst=0)
    

    time.time()

    获取当前时间的时间戳, 既表示从1970年1月1日00:00:00开始按秒计算的偏移量。

    time.sleep()

    推迟指定的时间运行下面的代码, 单位为秒

    datetime 模块

    datetime模块可以进行时间的加减

    import datetime
    
    # 获取当前时间
    now = datetime.datetime.now() 
    print(now)  # 2019-01-20 19:05:59.401263
    
    # 当前时间加三天
    print(now + datetime.timedelta(3))  # 2019-01-23 19:05:59.401263
    
    # 当前时间加三周
    print(now + date.time.timedelta(weeks=3)
          
    # 当前时间加三小时
    print(now + datetime.timedelta(hours=3))
          
    # 替换时间
    print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0))  # 1949-10-01 10:01:00
    
    

    random 模块

    import random
    
    # 0-1随机数
    print(random.random())  # 0.37486363608725715
    
    # 随机整数(包含两头)
    print(random.randint(1,3))  # 1
    
    # 打乱
    lis = [1, 2, 3] 
    random.shuffle(lis)
    print(lis)  # [1, 3, 2]
    
    # 随机选择一个
    print(random.choice(lis))  # 1
    
    # 让括号内放入不会变化的数据如2, 则每次打印结果相同; 放入一直变化的数据如time.time()则每次打印结果不同
    random.seed(2)
    print(random.random)
    
    # 随机选择样本
    print(random.sample(['a', 'b', 'c'],2)  # ['b', 'c']
    
    

    hashlib 模块 和 hmac 模块

    对字符加密: 字符---哈希算法---> 一串hash值

    特点:

    1. 只要传入内容一样, 得到的hash值就一样
    2. 不能由hash值反解成内容
    3. 只要hash算法不变, 得到的hash值长度是固定的
    import hashlib
    
    m = hashlib.md5()
    print(m)
    m.update(b'password')
    print(m.hexdigest())  # 5f4dcc3b5aa765d61d8327deb882cf99
    
    

    撞库破解hash算法加密

    # 已知密码可能为下面的一个
    
    real_pwd_hash = '5f4dcc3b5aa765d61d8327deb882cf99'
    
    pwd_list = [
        'password',
        'password123',
        '123passsword'
    ]
    
    find = False
    for pwd in pwd_list:
        
        m = hashlib.md5()
    	m.update(pwd.encoding('utf-8'))
        pwd_hash = m.hexdigest()
        
        if pwd_hash == real_pwd_hash:
            continue
         print(f'用户密码为: {pwd}')  
        find = True
            
    if not find:
        print('未发现密码')
    
    # 用户密码为: password
    

    hmac 模块可以对我们处理过的内容再次加密, 既相当于密匙, 可以防止被装库

    typing 模块

    与函数联用, 控制函数参数的数据类型, 提供了基础数据类型之外的数据类型

    
    from typing import Iterable, Iterator, Generator
    
    def func(x: int, lis:Iterable) -> list:
        
    

    requests 模块

    模拟浏览器对url发送请求, 拿到数据

    import requests
    
    response=requests.get(r'https://baidu.com')
    data = response.text
    print(data)
    

    re 模块

    正则表达式: 去字符串中找到符合某种特点的字符串

    元字符

    import re
    
    
    s = 'abcdabc'
    
    # ^ : 以...开头
    res = re.findall('^ab', s)
    print(res)
    
    # $ : 以...结尾
    res = re.findall('bc$',s)
    print(res)
    
    
    # . : 任意字符
    res = re.findall('abc.',s)
    print(res)
    
    # d : 数字
    s = 'abc123def'
    res = re.findall('d', s)
    print(res)
    
    # w : 非空, 数字字母下划线
    s = 'abc_123 456 def'
    res = re.findall('w', s)
    print(res)
    
    # s : 空, 空格,	和
    
    s = 'abc_123 456 def'
    res = re.findall('s', s)
    print(res)
    
    # + : 其前面的一个字符至少有一个
    s = 'abcddd abcd abc'
    res = re.findall('abcd+', s)
    print(res)
    
    # ? : 表示前面的一个字符至少为0个
    s = 'abcddd abcd abc'
    res = re.findall('abcd?', s)
    print(res)
    
    # [] : 中括号内元素的都可以
    s = 'abc bbc cbc dbc'
    print(re.findall('[abc]bc', s))
    
    # [^] : 中括号内元素的都不可以
    s = 'abc bbc cbc dbc'
    print(re.findall('[^abc]bc', s))
    
    # | : 或
    s = 'abc bbc cbc'
    print(re.findall('abc|bbc', s))
    
    # {2}:前面的一个字符有2个
    
    s = 'abccabc abccc'
    print(re.findall('abc{2}', s))
    
    # {1,2}:前面的一个字符有1个或者2个
    s = 'abccabc abccc'
    print(re.findall('abc{1,2}', s))
    
    

    贪婪匹配

    # 贪婪匹配: 默认返回符合条件的最大长度的字符
    # . 表示任意字符, * 表其前面的字符有0个或者无数个
    s = 'abcdzeeeeeeeeeeeeeez'
    res = re.findall('a.*z', s)
    print(res)  # ['abcdzeeeeeeeeeeeeeez']
    
    
    # 非贪婪匹配: 返回符合条件的最小长度字符
    # 使用问号 ? 即可进入非贪婪匹配 
    s = 'abcdzeeeeeeeeeeeeeez'
    res = re.findall('a.*?z', s)
    print(res)  # ['abcdz']
    

    match()函数 与 search()函数基本是一样的功能,不一样的就是match()匹配字符串开始位置的一个符合规则的字符串,search()是在字符串全局匹配第一个合规则的字符串

    # match: 从字符串头部开始寻找, 找不到就报错
    s = 'abc abcd abc'
    res = re.match('abcd', s)
    print(res.group())  # 报错
    
    # search: 从字符串全局寻找
    s = 'abc abcd abc'
    res = re.search('abcd', s)
    print(res.group())  # abcd
    

    re.S

    可以让. 匹配换行符

    s = '''abc
    abcabc*abc
    '''
    
    print(re.findall('abc.abc', s))  # ['abc*abc']
    print(re.findall('abc.abc', s, re.S))  # ['abc
    abc', 'abc*abc']
    

    分组和有名分组

    # 分组: 只要括号里面的
    s = 'abc abcd abcd'
    print(re.findall('a(.)c(d)', s))  # [('b', 'd'), ('b', 'd')]
    
    # 有名分组 ?P<>
    s = 'abc abcd abcd'
    print(re.search('a(?P<name1>.)c(?P<name2>d)', s).grounpdict())  # {'name1': 'b', 'name2': 'd'}
    
    
  • 相关阅读:
    git命令
    WPF让绑定支持表达式
    WPF多语言动态切换的一种方法
    C#监测系统事件,睡眠状态
    记一次渗透测试
    Relay
    ECShop相关漏洞复现/分析
    人工智能学习路线图
    抽奖算法
    关于微信开发的 appid,openid,unionid
  • 原文地址:https://www.cnblogs.com/bigb/p/11604952.html
Copyright © 2020-2023  润新知