FastAPI Websocket RPC 项目教程
2024-08-26 05:18:51作者:胡唯隽
1. 项目的目录结构及介绍
FastAPI Websocket RPC 项目的目录结构如下:
fastapi_websocket_rpc/
├── examples/
│ ├── basic_example.py
│ ├── proxy_example.py
│ └── ...
├── fastapi_websocket_rpc/
│ ├── __init__.py
│ ├── client.py
│ ├── endpoint.py
│ ├── methods.py
│ └── ...
├── tests/
│ ├── __init__.py
│ ├── test_client.py
│ ├── test_endpoint.py
│ └── ...
├── .gitignore
├── LICENSE
├── README.md
├── setup.py
└── ...
目录结构介绍
examples/: 包含项目的示例代码,如basic_example.py和proxy_example.py。fastapi_websocket_rpc/: 核心代码目录,包含客户端、服务端和方法处理等模块。__init__.py: 初始化文件。client.py: 客户端实现。endpoint.py: 服务端端点实现。methods.py: 方法处理实现。
tests/: 测试代码目录,包含各种测试用例。.gitignore: Git 忽略文件配置。LICENSE: 项目许可证。README.md: 项目说明文档。setup.py: 项目安装配置文件。
2. 项目的启动文件介绍
项目的启动文件通常位于 examples/ 目录下,例如 basic_example.py。以下是 basic_example.py 的示例代码:
from fastapi import FastAPI
from fastapi_websocket_rpc import RpcMethodsBase, WebsocketRPCEndpoint
app = FastAPI()
# 定义服务端方法
class ConcatServer(RpcMethodsBase):
async def concat(self, a: str = "", b: str = ""):
return a + b
# 添加 RPC 端点
endpoint = WebsocketRPCEndpoint(ConcatServer())
endpoint.register_route(app, "/ws")
# 启动服务
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
启动文件介绍
from fastapi import FastAPI: 导入 FastAPI 框架。from fastapi_websocket_rpc import RpcMethodsBase, WebsocketRPCEndpoint: 导入 RPC 相关模块。app = FastAPI(): 创建 FastAPI 应用实例。class ConcatServer(RpcMethodsBase): 定义服务端方法类。endpoint = WebsocketRPCEndpoint(ConcatServer()): 创建 RPC 端点实例。endpoint.register_route(app, "/ws"): 注册路由。if __name__ == "__main__":: 启动服务。
3. 项目的配置文件介绍
项目的配置文件主要是 setup.py,用于项目的安装和分发。以下是 setup.py 的示例代码:
from setuptools import setup, find_packages
setup(
name="fastapi_websocket_rpc",
version="0.1.26",
packages=find_packages(),
install_requires=[
"fastapi",
"websockets",
"tenacity",
],
author="Or Weis",
author_email="or@permit.io",
description="A fast and durable bidirectional JSON RPC channel over Websockets and FastApi",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/permitio/fastapi_websocket_rpc",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.7',
)
配置文件介绍
name: 项目名称。version: 项目版本。packages: 包含的包。- `install_
登录后查看全文
热门项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin08
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
537
3.75 K
暂无简介
Dart
773
191
Ascend Extension for PyTorch
Python
343
406
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.34 K
755
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.07 K
97
React Native鸿蒙化仓库
JavaScript
303
355
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
337
180
AscendNPU-IR
C++
86
141
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
986
248