标签:md5 git key bsp shel efault amp config iges
configparser模块
一句话概念:可以通过configparser将配置信息字典化的读写
import configparser
#生成一个这样的文档
config = configparser.ConfigParser() #生成一个对象
config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘,
‘Compression‘: ‘yes‘,
‘CompressionLevel‘: ‘9‘}
config[‘bitbucket.org‘] = {‘User‘: ‘hg‘}
config[‘topsecret.server.com‘] = {‘Port‘: ‘50022‘,
‘ForwardX11‘ : ‘no‘}
with open(‘example.ini‘, ‘w‘) as f:
config.write(f)
#读取 import configparser config = configparser.ConfigParser() #定义一个对象 config.read(‘example.ini‘) #读取文件 print(config.sections()) #显示除default之外的模块 for key in config[‘bitbucket.org‘]: print(key)
shelve模块
一句话概念:shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
写入
import shelve
d = shelve.open(‘shelve_test‘) #打开一个文件
def stu_data(name,age):
print("register",name,age)
name = ["alex","rain","test"]
info = {"name":"alex","age":22}
d["test"] = name
d["info"] = info
d[‘func‘] = stu_data
读取
import shelve
def stu_data(name,age):
print("stu",name,age)
d = shelve.open("shelev_test")
print(d.get("test"))
print(d.get("info"))
print(d.get("func")("test",30))
一句话概念:通过md等方式进行加密
import hashlib #md5算法,支持中文
m = hashlib.md5()
m.update(b"Hello") #加密
m.update("支持中文".encode("utf-8"))
print(m)
print(m.hexdigest())
import hmac #hmac算法,双层加密
h = hmac.new(b‘wueiqi‘,‘your are 250‘.encode("utf-8")) #自定义key
h.update(b‘hellowo‘) #加密
print(h.hexdigest())
一句话概念:用来生成一个随机数
常用语法
print(random.random()) #区间就0-1 print(random.uniform(1,10)) #指定区间 print(random.randint(1,5)) #取整数 print(random.randrange(1,10)) #不包含10 print(random.choice(‘hello‘)) #随机找一个值,也可以是列表,元祖 print(random.sample(‘hello‘,2)) #就取前两位
洗牌
items = [1,2,3,4,5,6,7] print(items) #[1, 2, 3, 4, 5, 6, 7] random.shuffle(items) print(items) #[1, 4, 7, 2, 5, 3, 6]
字母加数字的验证码
print(string.digits) #打印数字 print(string.ascii_letters) #打印字母 str_source = string.ascii_letters + string.digits print(random.sample(str_source,7))
生成一个验证码方法二
checkcode = ‘‘
for i in range(4):
current = random.randrange(0,4)
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
checkcode += str(temp)
print(checkcode)
configparser ,shelve ,hashlib,random模块
标签:md5 git key bsp shel efault amp config iges
原文地址:http://www.cnblogs.com/fengdao/p/6080188.html