python基于socketserver实现并发,验证客户端的合法性
作者:小杨-先生 时间:2023-04-15 09:05:38
目录
一、socketserver实现并发
二、验证客户端合法性
一、socketserver实现并发
tcp协议的socket是只能和一个客户端通信的,使用socketserver可以实现和多个客户端通信,他是在socket的基础上进行的封装,底层还是调用的socket。
socket是底层模块
socketserver是基于socket完成的
socketserver代码格式:
服务端:
import socketserver # 引入模块
import time
# 类名随便定义,但是必须继承socketserver.BaseRequestHandler此类。
class Myserver(socketserver.BaseRequestHandler):
def handle(self): # handle方法,固定名字
conn = self.request # conn相当于每个客户端连接过来的,客户端信息
'''
以下就是基于conn客户端信息,对每个客户端链接到服务端必须做的操作
conn——>等于客户端的连接信息
以此实现并发连接
'''
while True:
try:
content = conn.recv(1024).decode('utf-8')
conn.send(content.upper().encode('utf-8'))
time.sleep(1)
except ConnectionResetError:
break
# 对 socketserver.ThreadingTCPServer 类实例化对象,将IP地址,端口号,以及自己定义的类名传入,并返回一个对象
server = socketserver.ThreadingTCPServer(('127.0.0.1', 4444), Myserver)
server.serve_forever() # 执行对象server_forever方法,开启服务端
客户端:
import socket
sk = socket.socket()
sk.connect(('127.0.0.1', 4444))
while True:
sk.send(b'heelo')
content = sk.recv(1024).decode('utf-8')
print(content)
二、验证客户端合法性
在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂,那么利用hmac+加盐的方法实现。
思路:
服务端发送随机字符串(并用客户端和服务端都知道的秘钥对随机字符串进行加密)——>客户端接收到服务端发送的随机字符串(并用相同的秘钥进行加密发送回服务端)——>服务端接收到客户端发送的加密字符串和自己加密的字符串做对比(如果一致,就是合法客户端,不一致,就立即关闭)
使用hashlib方法:
服务端
import socket
import hashlib
import os
# 设置双方都拥有的秘钥
secret = '秘钥'.encode('utf-8')
sk = socket.socket()
sk.bind(('127.0.0.1', 4444))
sk.listen()
conn, _ = sk.accept()
# 给服务端发送32位随机字节串
count = os.urandom(32)
conn.send(count)
# 并对字节串进行加盐加密
hs = hashlib.sha1(secret)
hs.update(count)
info_server = hs.hexdigest()
# 接收客户端发送的对字节串加密后的信息
info_client = conn.recv(1024).decode('utf-8')
# 客户端加密后的信息和服务端加密后的信息做对比
if info_server == info_client:
# 相等则连接合法,继续操作
print('合法连接')
else:
# 不相等则断开链接
print('非合法用户')
conn.close()
客户端
import socket
import hashlib
# 双方共有的秘钥
secret = '秘钥'.encode('utf-8')
sk = socket.socket()
sk.connect(('127.0.0.1', 4444))
# 接收服务端发送的随机字节
count = sk.recv(1024)
# 对随机字节进行加密
hs = hashlib.sha1(secret)
hs.update(count)
info = hs.hexdigest().encode('utf-8')
# 把加密后的字节发送回服务端做判断
sk.send(info)
来源:https://www.cnblogs.com/XiaoYang-sir/p/14767541.html
标签:python,socketserver,并发,验证客户端
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vue实现菜单切换功能
2024-05-03 15:12:08
解决Python 命令行执行脚本时,提示导入的包找不到的问题
2022-05-22 23:58:29
![](https://img.aspxhome.com/file/2023/1/105341_0s.jpg)
python中必会的四大高级数据类型(字符,元组,列表,字典)
2023-01-10 00:54:20
![](https://img.aspxhome.com/file/2023/3/75373_0s.jpg)
微信小程序webview组件交互,内联h5页面并网页实现微信支付实现解析
2023-07-02 05:30:09
![](https://img.aspxhome.com/file/2023/9/137069_0s.png)
Python实现一键下载视频脚本
2023-08-26 18:51:17
![](https://img.aspxhome.com/file/2023/8/110218_0s.jpg)
快速了解Python开发环境Spyder
2023-01-29 14:37:47
![](https://img.aspxhome.com/file/2023/3/121123_0s.jpg)
Python随机生成彩票号码的方法
2023-10-13 15:25:39
简化翻页提升可用性
2009-03-28 11:52:00
![](https://img.aspxhome.com/file/UploadPic/20093/28/wp_pagenavi_1_en-28s.png)
深入讲解Python中的迭代器和生成器
2021-11-29 05:32:56
![](https://img.aspxhome.com/file/2023/1/72201_0s.jpg)
vue实现父子组件之间的通信以及兄弟组件的通信功能示例
2024-05-21 10:15:43
![](https://img.aspxhome.com/file/2023/4/125294_0s.gif)
python数组中的 k-diff 数对例题解析
2022-03-30 18:21:47
![](https://img.aspxhome.com/file/2023/7/65927_0s.png)
基于Python实现图像文字识别OCR工具
2022-10-19 04:44:42
![](https://img.aspxhome.com/file/2023/5/90505_0s.jpg)
数据库查询哪个对像里面包含什么字段方法语句
2024-01-24 10:32:56
python3安装pip3(install pip3 for python 3.x)
2023-03-30 07:19:09
![](https://img.aspxhome.com/file/2023/6/75286_0s.png)
解析Python中while true的使用
2022-07-23 21:19:53
MySQL8.0中binlog的深入讲解
2024-01-16 23:22:02
![](https://img.aspxhome.com/file/2023/2/96492_0s.png)
Go语言学习笔记之错误和异常详解
2024-02-05 11:39:15
![](https://img.aspxhome.com/file/2023/3/114803_0s.jpg)
Python中使用strip()方法删除字符串中空格的教程
2022-08-30 16:16:09
Python生成随机数的方法
2022-10-18 12:57:28
Go 值传递与引用传递的方法
2023-06-25 03:11:11
![](https://img.aspxhome.com/file/2023/3/89023_0s.png)