server端
import socket import os import hmac seret_key = b‘egg‘ sk = socket.socket() sk.bind((‘127.0.0.1‘,8080)) sk.listen() def check_conn(conn): msg = os.urandom(32) conn.send(msg) h = hmac.new(seret_key,msg) digest = h.digest() client_digest = conn.recv(1024) return hmac.compare_digest(digest,client_digest) conn,addr = sk.accept() res = check_conn(conn) if res: print(‘合法的客户端‘) conn.close() else: print(‘不合法的客户端‘) conn.close() sk.close()
client端
import socket import hmac seret_key = b‘egg‘ sk = socket.socket() sk.connect((‘127.0.0.1‘,8080)) msg = sk.recv(1024) h = hmac.new(seret_key,msg) digest = h.digest() sk.send(digest) sk.close()
原文地址:https://www.cnblogs.com/kuraki/p/9575218.html
时间: 2024-11-14 12:45:22