def run_right(): option = [("grpc.max_send_message_length", 512 * 1024 * 1024), ("grpc.max_receive_message_length", 512 * 1024 * 1024)] with grpc.insecure_channel('127.0.0.1:8080', options=option) as channel: stub = http_pb2_grpc.HttpServiceStub(channel) response = stub.Echo(http_pb2.HttpRequest(size=4 * 1024 * 1024 - 10)) print(response)
def test4(): # import data2_pb2 # PB_FILE_PATH_LIST = ['http.pb', 'test4.pb', 'HotMessage.pb', 'anthor.pb', 'test2.pb', 'demo3.pb', # 'UDP.pb', 'cover_helloworld.pb', 'dog.pb', 'Message.pb', 'data.pb', 'example2.pb', # 'msginfo.pb', 'GroundData.pb', 'person.pb', 'message2.pb', 'protodemo.pb', 'user.pb'] import http_pb2 PB_FILE_PATH_LIST = [ 'data2.pb', 'test6.pb', 'test4.pb', 'simple.pb', 'test2.pb', 'GpsLocation.pb', 'dog.pb', 'SensorData.pb', 'common2.pb' ] for PB_FILE_PATH in PB_FILE_PATH_LIST: print(PB_FILE_PATH) print('-------') # 读message # message = data2_pb2.Message() message = http_pb2.HttpRequest() f = open(PB_FILE_PATH, "rb") message.ParseFromString(f.read()) f.close() from google.protobuf.json_format import MessageToJson # 关键 json = MessageToJson(message) print(json) print('-------')
def __handle_http_request(self, seqno, packet): request = http_pb2.HttpRequest() request.ParseFromString(packet) if request.method not in self.uri_handlers: raise RuntimeError("No URI handler registered for request {} {}".format(request.method, request.uri)) if request.uri not in self.uri_handlers[request.method]: raise RuntimeError("No URI handler registered for request {} {}".format(request.method, request.uri)) self.uri_handlers[request.method][request.uri](self, request)
import http_pb2 PB_FILE_PATH = 'http.pb' JSON_FILE_PATH = 'http.json' request = http_pb2.HttpRequest() request.major_version = 1 request.minor_version = 2 request.url = '/index.html' host = http_pb2.StringStringPair() host.first = 'Host' host.second = 'www.example.com' request.headers.append(host) # 以二进制形式保存 f = open(PB_FILE_PATH, "wb") f.write(request.SerializeToString()) f.close() # 读request request = http_pb2.HttpRequest() f = open(PB_FILE_PATH, "rb") request.ParseFromString(f.read()) f.close() from google.protobuf.json_format import MessageToJson # 关键 json = MessageToJson(request) print(json) # 保存json f = open(JSON_FILE_PATH, "w") f.write(json)