Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use PB in HTTP Protocol

Shulou Source: shulou.com Published: 2022-06-02 11:56:30 10月04日 Update

Editor to share with you how to use PB in the HTTP agreement, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Client code

In this example, the communication mode of http+json and that of http+Protocol Buffer are demonstrated respectively.

The test cases in this example are managed by the qtaf framework, and the practical application can be based on the requirements, focusing only on the core logic.

#-*-coding: utf-8-*-from testbase.testcase import TestCasefrom testbase import datadrivefrom testbase.retry import Retryimport requests Jsonimport sysfrom test_pb2 import Personclass Case001 (TestCase):''http_client' 'owner = "enbowang" status = TestCase.EnumStatus.Ready priority = TestCase.EnumPriority.Normal timeout = 1 # enter the core logic def run_test (self): # json simulates self.start_step ("http+json request test") url = "http://127.0." 0.1:8080/http_json "body = b'{" name ":" xx.xxx "} 'response = requests.post (url Data=body) self.log_info ("body:" + str (body)) self.log_info ('response status:' + str (response.status_code)) self.log_info ('response content:' + str (response.text)) # Protocol Buffer simulation For the definition of PB format, see the previous article in this series: self.start_step ("http+Protocol Buffer request testing") url = "http://127.0.0.1:8080/http_proto" person = Person () person.name =" xx.xxx "person.id = 123456 body = person.SerializeToString () response = requests.post (url) Data=body) self.log_info ("body:" + str (body)) self.log_info ('response status:' + str (response.status_code)) self.log_info ('response content:' + str (response.text)) if _ _ name__ = ='_ main__': Case001 (). Debug_run () server code

The server is implemented using webpy

The parsing of json data and PB data are realized respectively.

# coding:utf-8import web,jsonfrom test_pb2 import Personurls = ('/ http_json', 'index',' / http_proto' 'pb') # json request to enter the logic class index: def GET (self): return "Hello" def POST (self): data = web.data () result = json.loads (data) return result [' name'] # pb request to enter the logic class pb: def GET (self): return "Hello" def POST (self): data = web. Data () person = Person () person.ParseFromString (data) # deserialize return person.nameapp = web.application (urls Globals () if _ _ name__ = = "_ _ main__": the app.run () client runs as follows

The above is all the contents of the article "how to use PB in HTTP Agreement". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Content way article logic test code customer client data core state service communication not much most actual sequence more format framework Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Huawei NVidia MariaDB