Python – 使用flask_sockets库构建websocket服务器
1 安装flask_sockets
conda好像装不了,使用pip安装
pip install Flask-Sockets
2 创建websocket服务器
2.1 普通方式
使用以下代码创建一个简单的websocket服务器,服务器地址为:ws://localhost:5678/
# -*- coding: utf-8 -*-
from flask import Flask
from flask_sockets import Sockets
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
app = Flask(__name__)
sockets = Sockets(app)
@sockets.route('/')
def run(ws):
while not ws.closed:
# 接收发送过来的消息
message = ws.receive()
response_text = f"Server receive message: {message}"
# 向客户端发送消息
ws.send(response_text)
@app.route('/')
def hello():
return 'Hello World!'
if __name__ == "__main__":
server = pywsgi.WSGIServer(('localhost', 5678), app, handler_class=WebSocketHandler)
server.serve_forever()
2.2 蓝图方式
# -*- coding: utf-8 -*-
from flask import Flask, Blueprint
from flask_sockets import Sockets
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
html = Blueprint(r'html', __name__)
ws = Blueprint(r'ws', __name__)
@html.route('/')
def hello():
return 'Hello World!'
@ws.route('/')
def echo_socket(socket):
while not socket.closed:
message = socket.receive()
response_text = f"Server receive message: {message}"
socket.send(response_text)
app = Flask(__name__)
sockets = Sockets(app)
app.register_blueprint(html, url_prefix=r'/')
sockets.register_blueprint(ws, url_prefix=r'/')
if __name__ == "__main__":
server = pywsgi.WSGIServer(('localhost', 5678), app, handler_class=WebSocketHandler)
server.serve_forever()
3 在线测试
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Python – 使用flask_sockets库构建websocket服务器
原文链接:https://www.stubbornhuang.com/1440/
发布于:2021年07月16日 14:42:29
修改于:2023年06月26日 21:30:01
当前分类随机文章推荐
- Python - 列表list遍历方法总结 阅读724次,点赞0次
- Python - 获取当前py脚本文件所在的目录路径 阅读975次,点赞0次
- Python - 获取指定文件夹及其所有子文件夹、孙文件夹下指定文件类型的文件路径 阅读401次,点赞0次
- Python - 类对象/列表/元祖/字典判空的方法 阅读2582次,点赞0次
- Python3 - 正则表达式去除字符串中的特殊符号 阅读13926次,点赞1次
- 解决Python爬虫在爬资源过程中使用urlretrieve函数下载文件不完全且避免下载时长过长陷入死循环,并在下载文件的过程中显示下载进度 阅读4398次,点赞0次
- Python - 使用python-opencv裁剪原视频为与视频高同宽的视频 阅读1821次,点赞0次
- Python - 使用Python+websockets时报错:AttributeError: module 'websockets' has no attribute 'serve' 阅读1873次,点赞0次
- Python - list/numpy/pytorch tensor相互转换 阅读1971次,点赞0次
- Python - 安装onnxruntime-gpu出现ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '...\\numpy-1.23.1.dist-info\\METADATA' 阅读1007次,点赞0次
全站随机文章推荐
- nginx - 反向代理websocket服务 阅读32次,点赞0次
- C++ - Windows/Linux生成uuid(通用唯一识别码) 阅读2809次,点赞2次
- 资源分享 - Real-Time 3D Rendering with DirectX and HLSL - A Practical Guide to Graphics Programming 英文高清PDF下载 阅读2810次,点赞0次
- 资源分享 - Real-Time 3D Character Animation with Visual C++ 英文高清PDF下载 阅读1915次,点赞0次
- TensorRT - 使用torch普通算子组合替代torch.einsum爱因斯坦求和约定算子的一般性方法 阅读3479次,点赞3次
- Visual Studio - 借助远程Linux服务器环境在Visual Studio中编写和远程调试Linux C++程序 阅读1533次,点赞0次
- 资源分享 - Color Imaging - Fundamentals and Applications 英文高清PDF下载 阅读1591次,点赞0次
- Windows远程桌面复制粘贴功能失灵的解决方法 阅读350次,点赞0次
- OpenCV - 新建一个图片,并在图片上画由一点到另一点的直线,采用反走样形式 阅读3235次,点赞0次
- C++ - 函数返回多个返回值的方法总结 阅读2624次,点赞0次
评论
169