您如何使用 gRPC python auth 库进行客户端和服务器身份验证?这些文档仅涵盖服务器身份验证。
是否grpc.secure_channel()需要使用其他标志?
我的 Web 应用程序向可扩展服务代理(ESP) 发出 HTTP 请求,后者又委托给gRPC 服务器(用 Python 编写)。忽略Android和iOS客户端,架构是:
ESP 是一个nginx 反向代理。
gRPC 服务器(参考架构中的“您的代码”)可能会引发异常,在这种情况下,我使用context.abort引发异常并以非正常状态终止 RPC:
try:
# Do something that could fail.
except ValueError as e:
context.abort(grpc.StatusCode.DATA_LOSS, str(e))
Run Code Online (Sandbox Code Playgroud)
虽然可以使用set_code和set_details,但它们仍然会导致 HTTP 状态为 200 OK。
有两个问题:
gRPC状态代码由 ESP 容器(nginx 代理)转换为通用500 Internal Server Error.
随附的详细信息已被删除。
2. 组合意味着 Web 客户端至多有一个500 Internal Server Error针对gRPC 服务器引发的所有异常的情况。
最终,我不明白如何将更多信息(理想情况下,自定义)错误返回给网络客户端。
python google-cloud-platform nginx-reverse-proxy grpc-python
我有一个服务需要将文件流返回给调用客户端,所以我创建了这个 proto 文件。
service Sample {
rpc getSomething(Request) returns (stream Response){}
}
message Request {
}
message Response {
bytes data = 1;
}
Run Code Online (Sandbox Code Playgroud)
当服务器收到这个时,它需要读取一些 source.txt 文件,然后将其作为字节流写回客户端。只是想问一下这是在 Python GRPC 服务器中执行此操作的正确方法吗?
fileName = "source.txt"
with open(file_name, 'r') as content_file:
content = content_file.read()
response.data = content.encode()
yield response
Run Code Online (Sandbox Code Playgroud)
我找不到任何与此相关的示例。
我有一个 gRPC HelloWorld 服务器运行,如官方入门指南(https://grpc.io/docs/quickstart/python/)中所示。我现在想从客户端关闭/终止服务器,可能是通过调用服务器方法。
我知道这是可以做到的,因为我阅读了这篇关于如何在 C++ 中做到这一点的文章。如何从客户端关闭 gRPC 服务器(使用 RPC 功能)
我的编程语言是客户端和服务器的python。任何帮助都感激不尽。
先问问题,再看上下文。如何使用异步 gRPC python 服务器基于从客户端取消 RPC 来执行某些服务器端操作(例如清理)?
在我的微服务中,我有一个asynciogRPC 服务器,其主要 RPC 是双向流。
在客户端(也使用 asyncio),当我取消某些操作时,它会引发一个asyncio.CancelledError被捕获且不会被grpc核心重新引发的异常:
except asyncio.CancelledError:
_LOGGER.debug('RPC cancelled for servicer method [%s]', _decode(rpc_state.method()))
Run Code Online (Sandbox Code Playgroud)
asyncio.CancelledError所以我不能依赖于在我自己的代码中捕获,因为它是预先捕获的并且不会重新引发。
共享上下文应该包含有关 RPC 是否在客户端被取消的信息,通过.cancel()从 RPC 调用进行调用并能够通过调用查看它是否被取消.cancelled():
https://grpc.github.io/grpc/python/grpc_asyncio.html#shared-context
抽象取消()
Run Code Online (Sandbox Code Playgroud)Cancels the RPC. Idempotent and has no effect if the RPC has already terminated. Returns A bool indicates if the cancellation is performed or not. Return type bool摘要取消()
Run Code Online (Sandbox Code Playgroud)Return True if the RPC is cancelled. The …
我有一个名为的目录protos,其中包含单个.proto文件,但最终会包含多个文件。该目录有一个名为 的同级目录app,我想在其中转储构建的grpcpython 文件。
我正在尝试编写一个简单的 bash 脚本,该脚本将调用protoc命令并.proto在 中构建文件protos,并将构建的文件输出到app
下面是tree我放置每个文件的位置。
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build_protos.sh\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 trainingInstance (root module directory)\n |\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.py\n | \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 (where I\'d like my built files to go)\n | |\n | \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80trainingInstance\n | \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80protos\n | \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80(where the built files actually go)\n |\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 protos\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 TrainingService.proto (what I\'d like to compile)\n\n\nRun Code Online (Sandbox Code Playgroud)\n\n当我运行时build_protos.sh,文件TrainingService.proto被检测到正常并构建,但不是将构建的文件放入...trainingInstance/app/<file> …
我无法带着这种困惑继续前进,因为我不知道该怎么办。Python 中的大多数教程都会获取定义方法的主类名称,但当您只需要编写客户端时,不知道如何执行此操作。
我正在尝试在Python中使用grpc服务创建小型docker映像。为了了解其大小,我构建了一个基本的hello-world Python grpc服务。为了使它“小”,我使用了一个多阶段构建,从python:3.7-alpine开始,然后
1)为最终的python安装创建一个virtualenv
2)为grpc和protobuf添加必要的构建包
3)将virtualenv复制到基本安装
4)复制应用程序文件
泊坞窗文件如下:
FROM python:3.7-alpine as base
FROM base as builder
RUN adduser -D webuser
WORKDIR /home/webuser
RUN apk add --update \
gcc \
g++ \
make \
musl-dev \
python3-dev \
libc6-compat \
&& rm -rf /var/cache/apk/*
# create a virtual env
RUN python -m venv env
# install all requirements
RUN env/bin/pip install protobuf grpcio
FROM base
RUN adduser -D webuser
WORKDIR /home/webuser
COPY --from=builder /home/webuser/env/ env/
# copy the app files
COPY …Run Code Online (Sandbox Code Playgroud)