码迷,mamicode.com
首页 > 编程语言 > 详细

python3 time、random、hashlib模块

时间:2017-06-24 22:55:12      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:strong   当前时间   随机数   port   python3   暂停   对象   sample   shu   

一、时间模块

时间的几种形式:时间戳,结构化时间,字符串时间
import time
print(time.time())      # 仅仅是当前时间的时间戳  float
print(time.localtime())     # 时间对象
s = time.localtime()    # 结构化时间对象
s2 = time.gmtime()      # 结构化时间对象  UTC时间
print(s)
print(s2)

res0 = time.mktime(time.localtime())    # 将结构化时间  转化成  时间戳

res1 = time.localtime(1412414234)       # 将时间戳  转化成  结构化时间

res2 = time.strftime("%Y-%m-%d", time.localtime())  # 将结构化时间  转化成  字符串时间

res3 = time.strptime("1900:03:12", "%Y-%m-%d")      # 将字符串时间  转化成  结构化时间

res4 = time.asctime()       # 将结构化时间  转化成  固定合适字符串时间 ‘Sun Nov 25 10:03:43 1979‘

res5 = time.ctime()     # 将时间戳  转化成  固定格式字符串时间 ‘Sun Nov 25 10:03:43 1979‘

res6 = time.sleep()     # 睡几秒,暂停几秒

  

二、随机数模块

import random

print(random.random())      # (0, 1) float类型

print(random.uniform(1, 3))     # float类型

print(random.randint(1, 3)) # [1, 3]  int

print(random.randrange(1, 3))   #[1, 3) int

print(random.choice([1, 2, ‘hello‘]))  # 随机

print(random.sample((1, 5), 2))    # 列表元素任意两个组合


l = [111, 222, 333, 444]
# random.shuffle(l)
print(random.shuffle(l))

# 练习:五位数字大小写字母验证码
def valdate_code():
    ret = ""
    for i in range(5):
        num = random.randint(0, 9)
        alfa = chr(random.randint(97, 122))
        alfa2 = chr(random.randint(65, 90))
        s = random.choice([str(num), alfa, alfa2])
        ret = ret + s
    return ret
print(valdate_code())

 

三、摘要算法hashlib

摘要算法应用1:文件一致性校验
# md5计算
import
hashlib md5_obj=hashlib.md5() md5_obj.update(b"helloworld") print(md5_obj.hexdigest()) # fc5e038d38a57032085441e7fe7010b0
# 如果数据量大,可以分块调用update,下面的计算值跟直接计算‘yuanhelloworld’值一样
import
hashlib md5_obj=hashlib.md5() md5_obj.update(b"yuan") md5_obj.update(b"helloworld") print(md5_obj.hexdigest()) #423bf62926c0e5bfba81a94977fdb224

 

摘要算法应用2:登录
username                pwd

admin                 admin
4523453452345         4732467812364342423
4523453452345         31423149238147982


admin             -----------  4732467812364

adminjindongdf    -----------  4732467812364342423

root              -----------  892345789432

12345678          -----------  6732456784325


 加盐处理

salt
md5_obj=hashlib.md5("salt")
md5_obj.update("admin")      # saltadmin

  

python3 time、random、hashlib模块

标签:strong   当前时间   随机数   port   python3   暂停   对象   sample   shu   

原文地址:http://www.cnblogs.com/lucaq/p/7074620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!