tcc/scripts/protobuftest.py

31 lines
988 B
Python

import person_pb2
import asyncio
import aiohttp
async def fetch(session):
obj = get_object()
# async with session.post('http://172.25.96.1:9090/simulation/protobuf', data=obj.SerializeToString(),
async with session.post('http://127.0.0.1:5000/simulation/protobuf', data=obj.SerializeToString(),
headers={"content-type": "application/protobuf"}) as resp:
print(resp.status)
data = await resp.read()
receiveObj = person_pb2.Person()
receiveObj.ParseFromString(data)
print(receiveObj)
async def go(loop):
async with aiohttp.ClientSession(loop=loop) as session:
await fetch(session)
def get_object():
obj = person_pb2.Person()
obj.name = "John Doe"
obj.age = 30
obj.friends.extend(["Jane Doe", "John Smith"])
obj.email = "john.doe@email.com"
obj.phone = "123-456-7890"
return obj
loop = asyncio.get_event_loop()
loop.run_until_complete(go(loop))
loop.close()