Day16作业及默写
hashlib模块,写函数校验两个文件是否内容相同(如果这两个文件很大)
import hashlib
md5 = hashlib.md5()
md5.update(b'hello')
md5.update(b'world')
ret = md5.hexdigest()
print(ret)
md5 = hashlib.md5()
md5.update(b'helloworld')
ret = md5.hexdigest()
print(ret)
#对比文件一致性的时候还需要加盐?
# 一个文件的md5值和另一个文件的md5值是不是一样
#两个文件 为什么要加密去对比
#文件的一致性校验
#视频软件
# 下载完成之后 转转转 正在检测当前文件是否可用
# 这是一个损坏的文件 不能使用
import os
import hashlib
def file_md5(path):
filesize = os.path.getsize(path)
md5 = hashlib.md5()
with open(path,'rb') as f:
while filesize >= 4096:
content = f.read(4096)
md5.update(content)
filesize -= 4096
else:
content = f.read(filesize)
if content:
md5.update(content)
return md5.hexdigest()
def cmp_file(path1,path2):
return file_md5(path1) == file_md5(path2)
path1 = r'D:\s20\day18\视频\4.面向对象整理.mp4'
path2 = r'D:\s20\day18\视频\tmp.mp4'
ret = cmp_file(path1,path2)
print(ret)
import hashlib
def Check_Diff(file_pathI,file_pathII):
with open(file_pathI,mode='r',encoding='utf-8') as f1,\
open(file_pathII,mode='r',encoding='utf-8') as f2:
for lineI,lineII in zip(f1,f2):
SHAI = hashlib.sha256(lineI.encode('utf-8'))
SHAII = hashlib.sha256(lineII.encode('utf-8'))
if SHAI.hexdigest() != SHAII.hexdigest():
return '这两个文件不是同一个文件'
else:
return '这两个文件是同一个文件'
ret = Check_Diff('list.txt','list.txt')
print(ret)
hashlib做一个密文存储密码的注册登陆程序
import hashlib
def md5(username,password):
md5 = hashlib.md5(username[::-1].encode('utf-8'))
md5.update(password.encode('utf-8'))
return md5.hexdigest()
def get_line():
with open('userinfo', encoding='utf-8') as
for line in f:
user, pwd = line.strip().split(',')
yield user,pwd
def register():
flag = True
while flag:
username = input('user :')
password = input('passwd :')
for user,pwd in get_line():
if user == username:
print('您输入的用户名已经存在')
break
else:
flag = False
password = md5(username,password)
with open('userinfo',encoding='utf-8',mode='a') as f:
f.write('%s,%s\n'%(username,password))
def login():
username = input('user :')
password = input('passwd :')
for user,pwd in get_line():
if username == user and pwd == md5(username,password):
return True
ret = login()
if ret:
print('登陆成功')
import hashlib
def encrypt_action(username,password,action='login',encrypt_file='encrypt_file.txt'):
with open(encrypt_file,mode='a+',encoding='utf-8') as file:
userencrypt = hashlib.sha256(username.encode('utf-8'))
userencrypt.update(password.encode('utf-8'))
if action == 'register':
file.seek(0,0)
for line in file:
if line.strip() == userencrypt.hexdigest():
return '账号已经注册,请更换其他账号登陆.'
else:
file.write(str(userencrypt.hexdigest())+'\n')
return '用户注册成功'
elif action == 'login':
file.seek(0,0)
for line in file:
if line.strip() == userencrypt.hexdigest():
return '用户登陆成功'
else:
return '用户名或密码不对.'
else:
return 'Warning'
menu = '加密注册登陆程序'.center(20,'-') + '\n' +'''
1. 登陆系统
2. 注册账户
3. 退出程序
'''
exit_flag = 'False'
while exit_flag == 'False':
print(menu)
Choice = input('>>>').strip()
if Choice == '1':
username,password = input('UserName:').strip(),input('Password:').strip()
ret = encrypt_action(username,password)
print(ret)
elif Choice == '2':
username,password = input('UserName:').strip(),input('Password:').strip()
ret = encrypt_action(username,password,'register')
print(ret)
else:
exit_flag = 'True'
print('\n')
拼手气发红包函数

#怎么能第一个人和最后一个人能取到多少钱是平均概率
import random
def red_pac(money,num):
ret = random.sample(range(1,money*100),num-1)
ret.sort()
ret.insert(0,0)
ret.append(money*100)
for i in range(len(ret)-1):
value = ret[i+1] - ret[i]
yield value/100
g = red_pac(200,10)
for i in g:
print(i)
os模块,计算文件夹的总大小
- 这个文件夹里都是文件
- 这个文件夹里还有文件夹
import os
def GetFileSize(dirpath):
count_size = 0
for file in os.listdir(dirpath):
count_size += os.path.getsize(file)
return count_size
def bytes2human(n):
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
print(prefix)
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return '%.1f%s' % (value,s)
return '%sB' % n
Size = GetFileSize('.')
ret = bytes2human(Size)
print(ret)
import os
def size(path):
num = 0
list_name = os.listdir(path)
for i in list_name:
file_name = os.path.join(path,i)
if os.path.isdir(file_name):
num1 = size(file_name)
num += num1
else:
num += os.path.getsize(file_name)
return num
print(size(os.path.abspath('.')))
计算当前月的1号的时间戳时间
import time
def MonthOne():
NowTime = time.strftime('%Y-%m',time.localtime(time.time()))
DayOne = time.strptime(NowTime+'-1','%Y-%m-%d')
return time.mktime(DayOne)
ret = MonthOne()
print(ret)
正则表达式练习
- 匹配整数或者小数(包括正数和负数)
- 匹配年月日日期 格式2018-12-6
- 匹配qq号
- 长度为8-10位的用户密码 : 包含数字字母下划线
- 匹配验证码:4位数字字母组成的
- 匹配邮箱地址
1. -?\d+(\.\d+)?
2. \d{1,4}-(1[0-2]|0?[1-9])-(3[01]|[12]\d|0?[1-9])
3. [1-9]\d{4,11}
4. \w{8,10}
5. [\dA-Za-z]{4}
6. [\w\-\.]+@([a-zA-Z\d\-]\.)+[a-zA-Z\d]{2,6}
Day16作业及默写的更多相关文章
- Day29作业及默写
作业: 1\ 默写 黏包协议 2\ 上传大文件(文件\视频\图片) 3\ 和你的同桌调通 从你的计算机上传一个视频到你同桌的电脑上 4\ 进阶 : 带上登录 Server #Server #!/usr ...
- Day20作业及默写
1.请使用C3算法计算出链接图中的继承顺序-Link 一 graph BT id1[A]-->id2[B] id2[B]-->id6[F] id6[F]-->id7[G] id1[A ...
- Day11作业及默写
1.写函数,传入n个数,返回字典{'max':最大值,'min':最小值} 例如:min_max(2,5,7,8,4) 返回:{'max':8,'min':2}(此题用到max(),min()内置函数 ...
- Day10作业及默写
1,继续整理函数相关知识点,写博客. 2,写函数,接收n个数字,求这些参数数字的和.(动态传参) def func(*number): sum=0 for num in number: sum+=nu ...
- Day19作业及默写
三级菜单 menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {}, '汽车 ...
- Day14作业及默写
1.整理今天所学内容,整理知识点,整理博客. pass 2.画好流程图. pass 3.都完成的做一下作业(下面题都是用内置函数或者和匿名函数结合做出): pass 4.用map来处理字符串列表,把列 ...
- Day13作业及默写
1. 整理今天的博客,写课上代码,整理流程图. 博客链接--博客园 2. 写一个函数完成三次登陆功能: 用户的用户名密码从一个文件register中取出. register文件包含多个用户名,密码,用 ...
- Day9作业及默写
1,整理函数相关知识点,写博客. 2,写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. def func(obj): return obj[1::2] 3, ...
- Day8作业及默写
1,有如下文件,a1.txt,里面的内容为: 老男孩是最好的培训机构, 全心全意为学生服务, 只为学生未来,不为牟利. 我说的都是真的.哈哈 分别完成以下的功能: 将原文件全部读出来并打印. with ...
随机推荐
- python记录_day05 字典
字典 字典由花括号表示{ },元素是key:value的键值对,元素之间用逗号隔开 特点:1.字典中key是不能重复的 且是不可变的数据类型,因为字典是使用hash算法来计算key的哈希值,然后用哈希 ...
- win8.1安装密钥
https://zhidao.baidu.com/question/374064869043943484.html
- 4月12 php练习
php中输出 <?php echo'hello'; php中打印多个div <?php for($i=1;$i<=100;$i++) { ?> <div style=&q ...
- HBase之六:HBase的RowKey设计
数据模型 我们可以将一个表想象成一个大的映射关系,通过行健.行健+时间戳或行键+列(列族:列修饰符),就可以定位特定数据,Hbase是稀疏存储数据的,因此某些列可以是空白的, Row Key Time ...
- windos 开启openssl
前面我使用的是wampserver百度提示的软件,然后我卸载了,自己重新再官网上下载了一个比较新的版本,然后我按照的时候用默认路径,他的的都不用怎么配置,新版本都给你弄好了. 低版本的要在httped ...
- css层叠性冲突中的优先级
一.首先从CSS级别来进行优先级划分: CSS控制页面样式的四种方法: 1.行内样式 通过style特性 <p style=”color:#F00; background:#CCC; font- ...
- 什么是 开发环境、测试环境、生产环境、UAT环境、仿真环境
开发环境:开发环境是程序猿们专门用于开发的服务器,配置可以比较随意, 为了开发调试方便,一般打开全部错误报告. 测试环境:一般是克隆一份生产环境的配置,一个程序在测试环境工作不正常,那么肯定不能把它发 ...
- 从使用角度看 ReentrantLock 和 Condition
java 语言中谈到锁,少不了比较一番 synchronized 和 ReentrantLock 的原理,本文不作分析,只是简单介绍一下 ReentrantLock 的用法,从使用中推测其内部的一些原 ...
- PE文件结构解析
说明:本文件中各种文件头格式截图基本都来自看雪的<加密与解密>:本文相当<加密与解密>的阅读笔记. 1.PE文件总体结构 PE文件框架结构,就是exe文件的排版结构.也就是说我 ...
- Linux CPU信息和使用情况查看(CentOS)
一.CPU信息查看 cat /proc/cpuinfo| grep "physical id"| sort -u | wc -l #查看是物理CPU个数,-u和uniq都是去重作用 ...