当前位置:网站首页>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-WzAvEuw6-1658921915132)(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)))
边栏推荐
- Scrapy Spider源码分析
- Sword finger offer II 109. unlock the password lock
- 彻底理解位运算——左移、右移
- 中国首枚芯片邮票面世:内置120um超薄NFC芯片
- OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
- 2022年全国最新消防设施操作员(中级消防设施操作员)题库及答案
- Preliminary learning function (3rd blog)
- Pagoda panel construction novel CMS management system source code measurement - thinkphp6.0
- When CNN meets transformer cmt:revolutionary neural networks meet vision transformers
- Kotlin Android development novice tutorial
猜你喜欢

Powerbi time series analysis, prediction and visualization tutorial

adb remount of the / superblock failed: Permission denied

shared_ PTR and make_ Use of shared

Asp net MVC web development tutorial

Android-第十三节03xUtils-数据库框架(增删改查)详解

英语文章翻译-英语文章翻译软件-免费批量翻译

宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0

英文翻译葡萄牙语-批量英文转换葡萄牙语-各种语言免费互译转换

OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)

When CNN meets transformer cmt:revolutionary neural networks meet vision transformers
随机推荐
Failed to install app-debug.apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
用于异常检测的Transformer - InTra《Inpainting Transformer for Anomaly Detection》
Jestson nano Object detection
My second blog - C language
JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover
With the help of panel industry innovation, will FPGA become the best choice for TCON?
智能合约安全——溢出漏洞
Pagoda panel construction novel CMS management system source code measurement - thinkphp6.0
SaltStack之return与job管理
source insight项目导入和使用教程
VAE:变分自编码器的理解与实现
Powerbi time series analysis, prediction and visualization tutorial
SaltStack之数据系统
Smart contract security - overflow vulnerability
Rust 入门指南(crate 管理)
企业级分布式爬虫框架入门
为什么在telnet登入界面下没有日志输出?
The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
Investment of 3.545 billion yuan! Gree Group participates in Xiaomi industry fund
Rust 入门指南(modules 和工程结构)