示例#1
0
def op():
    try:
        obj = test_pb2.MyObj()
        obj.number = 9
        obj.name = 'USB'

        #Serialize
        sendDataStr = obj.SerializeToString()
        #print serialized string value
        print('serialized string:', sendDataStr)
        #------------------------#
        #  message transmission  #
        #------------------------#
        receiveDataStr = sendDataStr
        receiveData = test_pb2.MyObj()

        #Deserialize
        receiveData.ParseFromString(receiveDataStr)
        print('pares serialize string, return: devId = ', receiveData.number,
              ', name = ', receiveData.name)
    except (Exception, e):
        print(Exception, ':', e)
        print(traceback.print_exc())
        errInfo = sys.exc_info()
        print(errInfo[0], ':', errInfo[1])
示例#2
0
async def fetch(session):
    obj = test_pb2.MyObj()
    obj.number = 9
    obj.name = 'USB'
    async with session.post('http://127.0.0.1:8081/', data=obj.SerializeToString(),
        headers={"content-type": "application/protobuf"}) as resp:
        print(resp.status)
        data = await resp.read()
        receiveObj = test_pb2.MyObj()
        receiveObj.ParseFromString(data)
        print(receiveObj)
示例#3
0
async def go(loop):
    obj = test_pb2.MyObj()
    obj.number = 9
    obj.name = 'USB'
    async with aiohttp.ClientSession(loop=loop) as session:
        await fetch(session)
requests.post("http://127.0.0.1:7070/ws/json/json/stuff",
              json={
                  "name": "Jennifer",
                  "number": 288000111
              }).json()

# In: Json
# Out: Protobuf to websocket clients
requests.post("http://127.0.0.1:7070/ws/json/pb/stuff",
              json={
                  "name": "Belle",
                  "number": 422222222
              }).json()

print("Sent: JSON, broadcast Protobuf to websocket clients")
send_obj = test_pb2.MyObj()
send_obj.number = 433678990
send_obj.name = 'Alicia'

# In: Protobuf
# Out: Protobuf to websocket clients
resp = requests.post(
    "http://127.0.0.1:7070/ws/pb/pb/stuff",
    headers={'content-type': 'application/protobuf'},
    data=send_obj.SerializeToString(),
)

print("\nSent protobuf data to server: ")
print(send_obj)
print(resp)