FastAPI Websocket RPC 项目教程
2024-08-26 03:39:13作者:胡唯隽
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_
登录后查看全文
热门项目推荐
kernelopenEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。C087
baihu-dataset异构数据集“白虎”正式开源——首批开放10w+条真实机器人动作数据,构建具身智能标准化训练基座。00
mindquantumMindQuantum is a general software library supporting the development of applications for quantum computation.Python057
PaddleOCR-VLPaddleOCR-VL 是一款顶尖且资源高效的文档解析专用模型。其核心组件为 PaddleOCR-VL-0.9B,这是一款精简却功能强大的视觉语言模型(VLM)。该模型融合了 NaViT 风格的动态分辨率视觉编码器与 ERNIE-4.5-0.3B 语言模型,可实现精准的元素识别。Python00
GLM-4.7GLM-4.7上线并开源。新版本面向Coding场景强化了编码能力、长程任务规划与工具协同,并在多项主流公开基准测试中取得开源模型中的领先表现。 目前,GLM-4.7已通过BigModel.cn提供API,并在z.ai全栈开发模式中上线Skills模块,支持多模态任务的统一规划与协作。Jinja00
agent-studioopenJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力TSX0137
Spark-Formalizer-X1-7BSpark-Formalizer 是由科大讯飞团队开发的专用大型语言模型,专注于数学自动形式化任务。该模型擅长将自然语言数学问题转化为精确的 Lean4 形式化语句,在形式化语句生成方面达到了业界领先水平。Python00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
473
3.5 K
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
213
87
暂无简介
Dart
719
173
Ascend Extension for PyTorch
Python
278
315
React Native鸿蒙化仓库
JavaScript
286
333
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
848
433
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.27 K
696
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
10
1
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
65
19