random模块

random.random()用于生成一个浮点数x,范围为0 =< x < 1

import random
>>>print(random.random())
1.864001829819306

random.uniform(a,b)用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。

import random

>>>print(random.uniform(1,10))
7.559074479037658
#其上限与下限参数位置可以改变,但生成的数值还是在范围之内
>>>print(random.uniform(10,1))
5.487835445265534

random.randint(a,b),用于生成一个范围内的整数,其中a必须大于等于b。

*
print(random.randint(1,5))
3
print(random.randint(5,1)
#引发一个ValueError

random.randrange([start],stop[,step])从一个集合中随机取出一个数字

import random

>>>print(random.randrange(1,100))
22
>>>print(random.randrange(1,100,2)) #偶数集合在这里等效于random.choice(range(1,100,2))
16

random.choice(seq), seq是一个非空序列,如list、tuple、字符串等。如果为空,引发IndexError

import random

>>>print(random.choice('abcdefg'))
c
>>>print(random.choice('tom','jerry','lisa'))
tom
>>>print(random.choice(('a','b','c')))
c

random.shuffle(x[,random]),将一个list打乱重新组合,list还是原list,没有占用空间。

import random

>>>alist = [1,2,3,4,5]
>>>random.shuffle(alist)
>>>print(alist)
[2, 5, 4, 3, 1]

random.sample(seq,k),从指定序列中随机获取指定长度的片段

import random

a = [1,2,3,4,5]
>>>print(random,sample(a,3))
[3,1,5]

random更多方法参见官方docs:random module

hashlib模块

这个模块实现了一个通用的接口来实现多个不同的安全哈希和消息摘要算法。包括FIPS安全散列算法SHA1,SHA224,SHA256,SHA384和SHA512(在FIPS 180-2中定义)以及RSA的MD5算法

因为哈希在字节上工作,而不是字符,所以要使哈希工作在字符上,需要encode编码为utf-8,再以十六进制形式进行摘要计算

import hashlib

str = 'mzc19971103'
md5 = hashlib.md5()
md5.update(str[0:5].encode('utf-8'))
md5.update(str[5:].encode('utf-8'))
print(md5.hexdigest()) sha1 = hashlib.sha1()
sha1.update('mzc19971103'.encode('utf-8'))
print(sha1.hexdigest())

处理字符串时还可以加盐,只要加盐字符串不被泄露,无法根据md5解析出字符串

import hashlib

def calc_md5(password):
md5 = hashlib.md5()
md5.update(password.encode('utf-8'))
a = md5.hexdigest()
return a db = {} def writeindb(username, password):
db[username] = calc_md5(password+'woaini1997') #加盐
print(db)
def login(username, password):
p = calc_md5(password+'woaini1997')
if username in db and db[username] == p:
print('login succ')
else:
print('username or passwd error') def register():
username = input('input ur username:')
password = input('input ur password:')
if username in db:
print('account exit!')
else:
writeindb(username, password)
def logininput():
username = input('plz input ur account:')
password = input('plz input ur password:')
if username != "" and password != "":
login(username, password)
else:
print('uname or passwd not empty!') if __name__ == '__main__':
while True:
# username = input('plz input ur username:\n')
# password = input('plz input ur password:\n')
selec =input('register select:1 login select:2 quit plz select:3\n')
if selec == '':
register()
elif selec == '':
logininput()
elif selec == '':
break

hashlib更多用法参见:Python-hashlib

base64

此模块提供将二进制数据编码为可打印ASCII字符并将此类编码解码回二进制数据的功能。它为在 RFC 3548中指定的编码提供编码和解码功能,该编码定义了Base16,Base32和Base64算法,以及事实上的标准Ascii85和Base85编码.

base64可以直接编码

import base64

def safe_base64_encode(a): #base64编码函数
s = base64.b64encode(a)
print(s) safe_base64_encode(b'legend')
#打印结果:
b'bGVnZW5k'

解码:

import base64

def safe_base64_decode(a):
s = base64.b64decode(a)print(a) safe_base64_decode(b'bGVnZW5k') #打印结果:
b'legend'

base64.b64encode()和base64.b64decode()参数必须是类字节对象

由于标准的Base64编码后可能出现字符+/,在URL中就不能直接作为参数,所以又有一种"url safe"的base64编码,其实就是把字符+/分别变成-_

>>> base64.b64encode(b'i\xb7\x1d\xfb\xef\xff')
b'abcd++//'
>>> base64.urlsafe_b64encode(b'i\xb7\x1d\xfb\xef\xff')
b'abcd--__'
>>> base64.urlsafe_b64decode('abcd--__')
b'i\xb7\x1d\xfb\xef\xff'

Base64编码原理与应用

Python3模块-random、hashlib和base64的更多相关文章

  1. Python全栈--7模块--random os sys time datetime hashlib pickle json requests xml

    模块分为三种: 自定义模块 内置模块 开源模块 一.安装第三方模块 # python 安装第三方模块 # 加入环境变量 : 右键计算机---属性---高级设置---环境变量---path--分号+py ...

  2. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  3. Python 自学基础(四)——time模块,random模块,sys模块,os模块,loggin模块,json模块,hashlib模块,configparser模块,pickle模块,正则

    时间模块 import time print(time.time()) # 当前时间戳 # time.sleep(1) # 时间延迟1秒 print(time.clock()) # CPU执行时间 p ...

  4. python3.7 random模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 random模块 import random #随机模块 # r ...

  5. python接口自动化测试二十七:密码MD5加密 ''' MD5加密 ''' # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'asdas89799,.//plrmf' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为

    python接口自动化测试二十七:密码MD5加密   ''' MD5加密 '''# 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import has ...

  6. 模块讲解---time模块,datetime模块,random模块,hashlib模块和hmac模块,typing模块,requests模块,re模块

    目录 1. 包 2. time模块   1. 优先掌握 2. 了解 3. datetime模块   1. 优先掌握 4. random模块   1. 优先掌握   2. 了解 5. hashlib模块 ...

  7. oldboy edu python full stack s22 day16 模块 random time datetime os sys hashlib collections

    今日内容笔记和代码: https://github.com/libo-sober/LearnPython/tree/master/day13 昨日内容回顾 自定义模块 模块的两种执行方式 __name ...

  8. Python3之random模块

    一.简介 ramdom模块提供了一个随机数的函数:random() 它可以返回一个随机生成的实数,范围在[0,1)范围内.需要注意的是random()是不能直接访问的,需要导入模块random才可以使 ...

  9. python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块

    正则表达式   语法:             mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...

随机推荐

  1. ngnix简介以及如何实现负载均衡原理

    1 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况.那么负载均衡的前提就是要有多台服务器才能实现, ...

  2. Robot Framework+AutoItLibrary+AutoIt使用

    使用记录: 1. 打开被测桌面程序: 2. 打开AutoIt,用finder tool拖拽到控件上,可以看到控件的信息: 3. 如果空间的Title.Control Info抓不到,可以看Mouse下 ...

  3. algorithm.sty not found error in LaTeX 解决方法

    参考: algorithm.sty not found error in LaTeX algorithm.sty not found error in LaTeX 解决方法 错误日志: LaTeX E ...

  4. 不消失的 taskeng 黑窗口?

    2017-01-06出来不消失的 taskeng 黑窗口? 计划运行某些程序时会出现这种现象.例如: 在计划中运行 a.bat : a.bat 里面的内容:start notepad.exestart ...

  5. Linux shell 计算两个文件的并集、交集、差集

    假设我们现在有两个文件 a.txt .b.txt a.txt 中的内容如下: a c 1 3 d 4 b.txt 中的内容如下: a b e 2 1 5 # Example 01 计算并集: [roo ...

  6. C# 缓存操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  7. shell中的${},##, %% , :- ,:+, ? 的使用

    假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值:${file#*/}:删掉第一个/ 及其左边的字符串:dir1/dir2 ...

  8. React-navigation物理返回键提示效果BackHandler

    componentWillMount(){    BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid); } co ...

  9. linux使用技巧,返回上一次目录

    cd - 当你一不小心,走岔了的时候,可以通过这个命令,直接找回上一次的路径.

  10. leecode第二十题(有效的括号)

    class Solution { public: bool isValid(string s) { ,end=s.size()-; )//万万没想到,他把空字符串当成true了 return true ...