1.udp循环发消息
# ### 客户端 import socket sk = socket.socket(type=socket.SOCK_DGRAM) while True: #发送消息 message = input("请输入客户端发送的消息") sk.sendto(message.encode('utf-8'),('127.0.0.1',9000)) #接收数据 msg,addr = sk.recvfrom(1024) print(msg.decode('utf-8')) sk.close()
# ### 服务端 import socket sk = socket.socket(type=socket.SOCK_DGRAM) #绑定地址 sk.bind(('127.0.0.1',9000)) #收发消息 while True: #接收消息 msg,cli_addr = sk.recvfrom(1024) print(msg.decode('utf-8')) print(cli_addr) message = input("服务端要发送的消息:") sk.sendto(message.encode('utf-8'),cli_addr)
2.socketserver
# ### 客户端 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()
# ### 服务端 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()
3.文件校验hashlib
import hashlib import random #基本用法 #(1) 创建一个md5算法的对象 hs = hashlib.md5() #(2) 把要加密的字符串通过update更新到hs对象中运算 hs.update('123456'.encode()) #里面的数据是二进制字节流 #(3) 获取32位16进制的字符串 res = hs.hexdigest() print(res,len(res)) #加盐(加key=>Xboy_ 加一个关键字配合原字符串,让密码更加复杂,不容易破解) hs = hashlib.md5('XBoy_^'.encode()) hs.update('123456'.encode()) res = hs.hexdigest() print(res,len(res)) #动态加盐 res = str(random.randrange(100000,1000000)) print(res) hs = hashlib.md5(res.encode('utf-8')) hs.update('123456'.encode()) res = hs.hexdigest() print(res) #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)) # ### 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))
# ### 文件校验 import hashlib hm = hashlib.md5() hm.update('123'.encode()) res = hm.hexdigest() print(res) ''' mode = r read(数字 -> 字符个数) mode = rb read(数字 -> 字节个数) with open('ceshi2.txt',mode='rb') as fp: res2 = fp.read(2) print(res2) ''' #(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(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) print(check_md5('ceshi1.txt')) print(check_md5('ceshi2.txt'))
4.服务器合法校验
# ### 服务端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()) secret_key = '芝麻开门' sk = socket.socket() sk.connect(('127.0.0.1',9000)) #验证服务端 auth(sk,secret_key) #发送数据 sk.send('请求调用支付宝刷脸支付接口'.encode()) #关闭连接 sk.close()
# ### 服务端2(支付宝) import socket import hamc import os def auth(conn,secret_key): #随机产生32位二进制字节流 msg = os.urandom(32) conn.send(msg) hm = hmac.new(secret_key.encode(),msg) res_server = hm.hexdigest() print(res_serve) res_client = conn.recv(1024).decode() if res_client == res_server: 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()