当前位置:网站首页>How does app automated testing achieve H5 testing
How does app automated testing achieve H5 testing
2022-07-28 19:35:00 【Zeze said test】
Microservices are inseparable from gRPC
Now most enterprises will adopt microservices as the software architecture , In the context of this architecture , Microservice frameworks and protocols are widely popular , and RPC It's also becoming popular . grpc Is based on RPC Framework , High performance , Very widely used .
grpc Developed and maintained by Google , Support almost all mainstream programming languages . No matter what you use Java, still Python, still Go, still Ruby wait , You can use it to realize remote services .

Protocol Buffers
grpc By default protocol buffers As a serialized transport format , Usually, the transmitted data type is represented by a with .proto Extension of the ordinary text file to store , Both the request and response data need to meet the data requirements defined here .
For example, when user authentication is carried out, it is often necessary to pass in the logged in user information , Server return token value , Corresponding proto File description , If the request or response data is too large , You can't get it all at once , Can pass stream Continuous transmission of streaming information , At this time, add stream keyword .
// login.proto
service User{
// login
rpc Login (LoginReqeust ) returns (LoginReply ) {}
// stream
rpc GetImage(LoginRequest) return (stream LoginReply ){}
}
// Login request data
message LoginReqeust {
string username = 1;
string passwd = 2;
}
// Login response data
message LoginReply {
string token = 1;
string msg = 2;
}
proto File generation gPRC Code
With Python For example , Installation is required first Python Related to the package .
pip install grpcio
pip install grpcio-tools
Corresponding generation Python Code commands , Be careful -I Express proto The subdirectory where the file is located :
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I protos login.proto
After entering the command , There will be two more files in the directory , One is called login_pb2.py, It reports the data formats related to requests and responses , There's another one called login_pb2_grpc.py, Classes that contain the client and the response side .
Generated login_pb2.grpc.py The code in …
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-f6pu4PtL-1658989710154)(C:\Users\muji\Desktop\logseq_local_D__logseq note _markdown_1658920736\logseq note _markdown_1658920736\assets\image_1658915681955_0.png)]
establish gRPC service
Creating an interface requires the above generated UserServicer class .
import login_pb2_grpc
import login_pb2
class User(login_pb2_grpc.UserServicer):
def Login(self, request, context):
return login_pb2.LoginReply(msg=f"hello, {
request.username}",
token="It is my token")
Operation service :
import grpc
from concurrent import futures
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
login_pb2_grpc.add_UserServicer_to_server(
User(), server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
serve()
establish gRPC client
An example :
import grpc
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = login_pb2_grpc.UserStub(channel)
response = stub.Login(login_pb2.LoginRequest(username='you', passwd="123"))
print("Client received: " + response.msg)
protocol buffer Turn into json
After the client gets the response result , You can get fields through class attributes :
response.msg
response.token
Sometimes we turn it into json Format , Easy to get the whole response . You can use protobuf This library , Install first :
pip install protobuf
Next, use the conversion to json Format method is ok .
from google.protobuf.json_format import MessageToJson
import json
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = login_pb2_grpc.UserStub(channel)
response = stub.Login(login_pb2.LoginRequest(username='you', passwd="123"))
print(json.loads(MessageToJson(response)))
边栏推荐
- 英文翻译意大利语-批量英文翻译意大利语工具免费
- Amazon launched Amazon one palm payment system, and the contactless palm vein recognition market is expected to explode
- Taking the opportunity of digital transformation, how can 3C enterprises achieve efficient collaboration through SRM supplier cloud collaboration platform?
- 文章翻译软件-批量免费翻译软件支持各大翻译接口
- 以数字化转型为契机,3C企业如何通过SRM供应商云协同平台实现高效协同?
- Adobe Flash player 34.0.0.92 and available version modification methods (2021-01-23
- Scrapy Spider源码分析
- source insight项目导入和使用教程
- lua语言的左对齐函数(手写)
- C language cycle sentence strengthening exercises
猜你喜欢

Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters

App自动化测试是怎么实现H5测试的

Rust 入门指南(modules 和工程结构)

shared_ PTR and make_ Use of shared

Adobe XD web design tutorial

文章翻译软件-批量免费翻译软件支持各大翻译接口
![[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements](/img/1c/6dbfcb5e6ade52d8cbfabcb34616a5.png)
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements

JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover

Rust 入门指南(crate 管理)

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
随机推荐
Scrapy Spider源码分析
C string to short[] method
Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
Transformer for anomaly detection - instra "painting transformer for anomaly detection"
使用SaltStack自动化部署LNMP
在矩池云快速安装torch-sparse、torch-geometric等包
Photoshop web design practical tutorial
Pytorch:交叉熵损失(CrossEntropyLoss)以及标签平滑(LabelSmoothing)的实现
【已解决】AC86U ML改版固件虚拟内存创建失败,提示USB磁盘读写速度不满足要求
用于异常检测的Transformer - InTra《Inpainting Transformer for Anomaly Detection》
为研发高端光刻胶,晶瑞股份斥资7500万元购买SK海力士的ASML光刻机
andorid系统layout、values、drawable适配
Salt SSH of saltstack
stc12c5a60s2功能说明(STC12C5A60S2默认触发)
Design of library management database system
NPM installing and uninstalling global packages
英文翻译意大利语-批量英文翻译意大利语工具免费
Adobe XD web design tutorial
SaltStack系统初始化
The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board