socketserver 实现TCP并发

socketserver 模块

# 使TCP协议支持并发操作
​
# 服务端
import socketserver
​
class MyServar(socketserver.BaseRequestHandler):                             # 必须继承 BaseRequestHandler 类
    def handle(self):                                                        # 必须定义handle函数.
        """
        收发数据的逻辑
        :return:
        """
        conn = self.request                                                  # 获取客户端消息
        cli_addr = self.client_address                                       # 获取客服端 ip+端口
        while True:                                                          # 循环
            # 接收消息
            msg = conn.recv(1024)                                            # 收发数据的逻辑
            print(msg.decode())
            # 发送消息
            data = input("发送消息")
            conn.send(data.encode())
​
servar = socketserver.ThreadingTCPServer(("127.0.0.1",9006),MyServar)       # 相当于创建,绑定,
servar.serve_forever()                                                      # While 循环一直监听是否有客户端请求到达 ...
​
# 客户端
import socket
​
sk = socket.socket()
sk.connect(("127.0.0.1",9006))
while True:
    # 发送消息
    data = input("发送消息")
    sk.send(data.encode())
    # 接收消息
    msg = sk.recv(1024)
    print(msg.decode())
​
sk.close()

  

hashlib & hmac 加密算法模块

# hashlib 加密算法模块
import hashlib
​
hm = hashlib.md5("加盐")                        # 创建md5对象并加盐
hm.updata("加密内容".encode())                  # 更新内容进行算法运算
res = hm.hexdigest()                           # 拿到运算后的32位16进制数
​
​
# hmac 加密算法模块
import hmac
​
hn = hmac.new("加盐".encode(),"数据".encode())
res = hn.hexdigest()
print(res)

  

动态加密

import hashlib,os     # rangdom也可以
​
salt = os.urandom(10000)               # 获取长度为10000的随机字节流
hm = hashlib.md5(salt)
hm.update("我爱你".encode())
res_hm = hm.hexdigest()
print(res_hm)

  

文件校验

import hashlib,hmac
​
# 针对小文件一次性读出来
def hashmd5_file(hm,filename):
    with open(filename, "rb") as f:
        data = f.read()
        hm.update(data)
        return hm.hexdigest()
​
obj1 = hashlib.md5()
obj2 = hashlib.md5()
hm_ceshi1 = hashmd5_file(obj1,"ceshi1.txt")
hm_ceshi2 = hashmd5_file(obj2,"ceshi2.txt")
​
if hm_ceshi1 == hm_ceshi2:
    print("校验合法")
else:
    print("不合法")
    
# 针对大文件分批进行
def hmac_file(hn,file_name):
    numsize = os.path.getsize(file_name)
    with open(file_name, "rb") as f:
        while numsize:
            data = f.read(128)
            hn.update(data)
            numsize -= len(data)
        return hn.hexdigest()
​
hn1 = hmac.new("盐".encode())
hn2 = hmac.new("盐".encode())
​
ceshi1 = hmac_file(hn1,"ceshi1.txt")
ceshi2 = hmac_file(hn2,"ceshi2.txt")
if ceshi1 == ceshi2:
    print("校验合法")
else:
    print("不合法")

  

服务器校验

# 模拟支付宝服务端
import socketserver,os,hmac
​
class MyServer(socketserver.BaseRequestHandler):
    miyu = "夜袭寡妇村"     # 密语
    def handle(self):
        conn = self.request
        cil_addr = self.client_address
​
        auth_code = os.urandom(128)
        conn.send(auth_code)
​
        cli_hmac = conn.recv(32).decode()
        hn = hmac.new(auth_code, MyServer.miyu.encode())
        servar_hmac = hn.hexdigest()
​
        if cli_hmac == servar_hmac:
            conn.send("支付成功~".encode())
        else:
            conn.send("滚~".encode())
​
​
servar = socketserver.ThreadingTCPServer(("127.0.0.1",3452),MyServer)
servar.serve_forever()
​
# 模拟合作服务端
​
import socket,hmac
# 密语
miyu = "夜袭寡妇村"
sk = socket.socket()
sk.connect(("127.0.0.1",3452))
​
auth_code = sk.recv(128)
hn = hmac.new(auth_code,miyu.encode())
​
msg = hn.hexdigest()
sk.send(msg.encode())
​
print(sk.recv(1024).decode())
​
sk.close()

  

 

 

posted @ 2020-08-23 18:55  bajie_new  阅读(200)  评论(0编辑  收藏  举报