一、时间模块

import time          # 模块调用语句   注意:模块级导入一般放在文件顶部
import datetime

print(time.time())    # 1550411181.441547: 时间戳
time.sleep(3)         # CPU不工作,停止3秒
print(time.clock())   # 7.331190182070108e-07   计算CPU工作的时间

print(time.gmtime())    # UTC世界标准时间(英国):time.struct_time(tm_year=2019, tm_mon=2,
# tm_mday=17, tm_hour=13, tm_min=54, tm_sec=45, tm_wday=6, tm_yday=48, tm_isdst=0)

print(time.localtime())   # 本地时间:time.struct_time(tm_year=2019, tm_mon=2, tm_mday=17,
# tm_hour=21, tm_min=59, tm_sec=18, tm_wday=6, tm_yday=48, tm_isdst=0)

print(time.strftime('%Y-%m-%d  %H:%M:%S'))   # (重要)字符串(格式化)时间:2019-02-17  22:03:39

print(time.strptime('2019-02-17  22:03:39', '%Y-%m-%d  %H:%M:%S'))   # 时间结构的转换,输出见下行:
# time.struct_time(tm_year=2019,tm_mon=2,tm_mday=17,tm_hour=22,tm_min=3,tm_sec=39,tm_wday=6,tm_yday=48,tm_isdst=-1
)

# 单独提取某个时间类型
a = time.strptime('2019-02-17  22:03:39', '%Y-%m-%d  %H:%M:%S')
print(a.tm_year)    # 年 2019
print(a.tm_mon)     # 月 2
print(a.tm_mday)    # 日 17
print(a.tm_hour)    # 时 22
print(a.tm_min)     # 分 3
print(a.tm_sec)     # 秒 39
print(a.tm_yday)    # 一年的第多少天 48
print(a.tm_wday)    # 一周的第多少天 6

print(time.ctime())   # 默认为空  打印当前时间:Sun Feb 17 22:18:13 2019
print(time.ctime(3600))  # 把一个数转换为时间  从1970年开始计算
print(time.mktime(time.localtime()))   # 把当前时间转换为时间戳:1550413367.0

print(datetime.datetime.now())   # 打印当前时间,友好表达方式:2019-02-28 14:33:37.570078

二、随机数模块

import random

print(random.random())          # 生成一个0-1的随机数
print(random.randint(1, 8))       # 随机生成一个1-8之间的随机整数(包括8)
print(random.choice('sadasfga'))      # 随机取出字符串中的一个字母
print(random.sample('asfafaffgh', 2))      # 从一个序列里面随机取出两个
print(random.randrange(1, 3))        # 1-3随机取出一个,不包括3

例:随机生成一个5位数的验证码,包含数字和字母

def v_code():
    code = ''
    for i in range(5):
        if i == random.randint(0, 3):
            add = random.randrange(10)   # 0-9随机取出一个数
        else:
            add = random.randrange(65, 90)   # 26个字母的ASCII值是65-90
            add = chr(add)  # 把ASCII转换为字母

        code += str(add)  # 字符串拼接

    print(code)

法二:比上述方法更加简单

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
def v_code():
    code = ''
    for i in range(5):
        add = random.choice([random.randrange(10), chr(random.randrange(65, 90))])
        code += str(add)   # 字符串拼接

    print(code)

v_code()

三、hashlib 加密模块

import hashlib   # 两种方法:MD5和SHA两种加密方法 不可逆结果

# MD5 算法
m = hashlib.md5()

m.update('hello world'.encode('utf8'))   # python3 默认utf8编码
print(m.hexdigest())   # 获取加密结果(16进制):5eb63bbbe01eeed093cb22bb8f5acdc3

m.update('long'.encode('utf8'))
print(m.hexdigest())   # 7e5181dfabb9edf08a7075a8c18b0343  在前一次转换的基础上进行转换,相当于对'hello worldlong'进行转换

m2 = hashlib.md5()
m2.update('hello worldlong'.encode('utf8'))
print(m2.hexdigest())   # 7e5181dfabb9edf08a7075a8c18b0343


# SHA 算法
m = hashlib.sha256()
m.update('hello world'.encode('utf8'))
print(m.hexdigest())   # 算法更复杂:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

四、sys模块

1.解释器进行交互:通过传入的参数决定程序执行那段代码

import sys      # 解释器进行交互

print(sys.argv)    # 输出的当前路径

def post():
    print('ok')

def download():
    pass

if sys.argv[1] == 'post':
    post()
elif sys.argv[1] == 'downlod':
    download()

打开电脑cmd,在文件路径下用python执行该程序

Python 模块学习(一) Python 第1张

输出列表,列表的第一个元素存的是文件名,可以在“python sys模块.py” 后面加参数控制程序的执行:

Python 模块学习(一) Python 第2张

注意:一般情况参数越多程序越灵活

2.常见使用:

import sys

sys.exit(0)  # 退出程序,可以加参数,正常退出是exit(0)

print(sys.platform)   # win32  返回windows的平台,可以做跨平台的判断
print(sys.version)   # 获取python解释器的当前版本信息
print(sys.path)    # 返回模块的搜索路径,初始化时使用pythonpath环境变量的值
print(sys.platform)     # 返回操作平台名称:win32
print(sys.path.append()) # 路径添加,如果要使用的模块不在python里,在另外的文件下,就需要添加绝对路径
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄