zoukankan      html  css  js  c++  java
  • 用hmac验证客户端的合法性

    服务器端程序

    import os
    import hmac
    import socket
    
    
    def auth(conn):
        secret_key = b'rock'
        rand_b = os.urandom(32)
        conn.send(rand_b)
        obj = hmac.new(secret_key, rand_b)
        res1 = obj.digest()
        res2 = conn.recv(1024)
        cmp_res = hmac.compare_digest(res1, res2)
        return cmp_res
    
    
    server = socket.socket()
    server.bind(('127.0.0.1', 9000))
    server.listen(5)
    
    conn, addr = server.accept()
    res = auth(conn)
    if res:
        print('是合法的客户端')
        conn.send('你好'.encode())
    else:
        conn.close()
    
    conn.close()
    server.close()

    客服端程序

    import hmac
    import socket
    
    
    def auth(client):
        secret_key = b'rock'
        rand_b = client.recv(32)
        obj = hmac.new(secret_key, rand_b)
        res2 = obj.digest()
        client.send(res2)
    
    
    client = socket.socket()
    client.connect(('127.0.0.1', 9000))
    auth(client)
    msg = client.recv(1024)
    print(msg.decode())
    client.close()
  • 相关阅读:
    Django模型层进阶
    Django模型层相关
    BOM与DOM
    JavaScript入门
    HTML基础
    子查询/联合分组/all与any/视图/事务
    表的查询
    mysql的多表关系
    Mysql基本操作
    Spider_基础总结2_Requests异常
  • 原文地址:https://www.cnblogs.com/chen55555/p/10318431.html
Copyright © 2011-2022 走看看