一.tcp多线程操作
引入socketserver模块
import socketserver
服务端
import socketserver
class MyServer(socketserver.BaseRequestHandler):
def handle(self):
# self.request => 建立三次握手返回的连接对象conn
# print(self.request)
# self.client_address => 客户端的ip端口号
# print(self.client_address)
conn = self.request
while True:
# 接受数据
msg = conn.recv(1024)
msg2 = msg.decode("utf-8")
print(msg2)
# 发送数据
conn.send(msg2.upper().encode("utf-8"))
server = socketserver.ThreadingTCPServer( ("127.0.0.1",9000) , MyServer )
server.serve_forever()
客户端
import socket
sk = socket.socket()
sk.connect( ("127.0.0.1",9000) )
# 收发数据的逻辑
while True:
sk.send(b"you can you up")
msg = sk.recv(1024)
print(msg.decode("utf-8"))
sk.close()
二.文件校验
1 .加密算法
import hashlib
(1)基本用法
1)创建一个md5算法对象
hs = hashlib.md5()
2)把要加密的字符串通过update更新到hs对象中运算
hs.update("123456".encode("utf-8")) (里面的数据为二进制字节流)
3)获取32位16进制的字符串
res = hs.hexdigest()
print(res,len(res))
(2)加盐
加一个关键字配合原字符串,为了让密码更加负载,不容易被破解
hs = hashlib.md5("XBoy_^".encode())
hs.update("123456".encode())
res = hs.hexdigest()
print(res,len(res))
动态加盐
import random
res = str(random.randrange(100000,1000000))
print(res)
hs = hashlib.md5(res.encode("utf-8"))
hs.update("123456".encode())
res = hs.hexdigest()
print(res)
(3)sha算法
sha 算出来的十六进制的串是40位,加密稍慢,安全性稍高
md5算出来的十六进制的串是32位,加密速度快,安全性一般
hs = hashlib.sha1()
hs.update("123456".encode())
res = hs.hexdigest()
print(res, len(res))
hs = hashlib.sha512()
hs.update("123456".encode())
res = hs.hexdigest()
print(res, len(res))
(4) hmac
hmac加密算法更加复杂,不容易破解
import hmac
# 必须指定盐
key = b"abc"
# 密码
msg = b"123456"
hm = hmac.new(key,msg)
res = hm.hexdigest()
print(res,len(res))
动态加盐
import os
"""
# 基本使用
# urandom 返回随机的二进制字节流, 参数:代表的长度
res = os.urandom(64)
print(res,len(res))
"""
key = os.urandom(32)
msg = b'123456'
hm = hmac.new(key,msg)
res = hm.hexdigest()
print(res,"<1>",len(res))
2 .文件校验
mode = r read(数字-> 字符个数)
mode = rb read(数字-> 字节个数)
(1)针对于小文件进行内容校验
def check_md5(file):
with open(file, mode="rb") as fp:
hs = hashlib.md5()
hs.update(fp.read())
return hs.hexdigest()
res1 = check_md5("ceshi1.txt")
res2 = check_md5("ceshi2.txt")
print(res1,res2)
(2)针对于大文件进行内容校验
hs = hashlib.md5()
hs.update("今天是星期一".encode())
res = hs.hexdigest()
print(res) #a33fc073e6be76154e58874c4ac7cee1
hs = hashlib.md5()
hs.update("今天是".encode())
hs.update("星期一".encode())
res = hs.hexdigest()
print(res) # a33fc073e6be76154e58874c4ac7cee1
方法一
def check_md5(file):
hs = hashlib.md5()
with open(file,mode="rb") as fp:
while True:
# read(5) 一次最多读取5个字节,
content = fp.read(5)
# 如果有内容就进行计算
if content:
# 分批进行更新计算;
hs.update(content)
else:
break
return hs.hexdigest()
print("<=====>")
print(check_md5("ceshi1.txt"))
print(check_md5("ceshi2.txt"))
方法二
import os
def check_md5(file):
hs = hashlib.md5()
# 计算文件大小返回的字节的个数
file_size = os.path.getsize(file) # 计算文件大小
with open(file,mode="rb") as fp:
while file_size:
# 一次最多5个
content = fp.read(5)
hs.update(content)
# 按照实际的读取个数进行相减;
file_size -= len(content)
return hs.hexdigest()
print("<=====>")
print(check_md5("ceshi1.txt"))
print(check_md5("ceshi2.txt"))
三.服务器合法性校验
服务端1
import socket
import hmac
import hashlib
def auth(sk,secret_key):
# 处理权限验证的逻辑
msg = sk.recv(32)
hm = hmac.new(secret_key.encode(),msg)
"""
# 在不知道加密算法的时候,只知道密钥也没用;
hs = hashlib.md5(secret_key.encode())
hs.update(msg)
res = hs.hexdigest()
"""
res = hm.hexdigest()
print(res)
sk.send(res.encode("utf-8"))
secret_key = "芝麻开门"
sk = socket.socket()
sk.connect( ("127.0.0.1" , 9000) )
# 验证服务端
auth(sk,secret_key)
# 发送数据
sk.send("请求调用支付宝刷脸支付接口".encode("utf-8"))
sk.close()
服务端2(支付宝)
import socket
import hmac
import os
def auth(conn,secret_key):
# 随机产生32位二进制字节流
msg = os.urandom(32)
conn.send(msg)
hm = hmac.new(secret_key.encode(),msg)
res_serve = hm.hexdigest()
print(res_serve)
res_client = conn.recv(1024).decode("utf-8")
if res_client == res_serve:
print("是合法的连接用户")
return True
else:
print("是不合法的连接用户")
return False
sk = socket.socket()
sk.bind( ("127.0.0.1" , 9000) )
sk.listen()
# 三次握手
conn,addr = sk.accept()
# 设置密钥
secret_key = "芝麻开门"
res = auth(conn,secret_key)
# 权限的校验
if res:
print(conn.recv(1024).decode("utf-8"))
# 四次挥手
conn.close()
# 退换端口
sk.close()