Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PB in HTTP Protocol

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report