服务端server.py
import os
import socket
import hashlib
def get_md5(key, seq):
md5_obj = hashlib.md5(key)
md5_obj.update(seq)
res = md5_obj.hexdigest()
return res
tcp_server = socket.socket()
tcp_server.bind(("127.0.0.1", 5060))
tcp_server.listen()
secret_key = b'saiya6'
rand_seq = os.urandom(32)
md5code = get_md5(secret_key, rand_seq)
conn, addr = tcp_server.accept()
conn.send(rand_seq)
ret = conn.recv(32).decode('utf-8')
if ret == md5code:
print("合法客户端")
else:
print("非法客户端")
conn.close()
tcp_server.close()
客户端client.py
import socket
import hashlib
def get_md5(key, seq):
md5_obj = hashlib.md5(key)
md5_obj.update(seq)
res = md5_obj.hexdigest()
return res
tcp_client = socket.socket()
tcp_client.connect(('127.0.0.1', 5060))
secret_key = b'saiya6'
rand_seq = tcp_client.recv(32)
md5code = get_md5(secret_key, rand_seq)
tcp_client.send(md5code.encode('utf-8'))
tcp_client.close()
标签:合法性,obj,socket,seq,验证,tcp,key,客户端,md5
From: https://www.cnblogs.com/saiya6/p/16948944.html