Skip to content

Instantly share code, notes, and snippets.

git clone https://github.com/salrashid123/grpc_curl
cd grpc_curl/src/
virtualenv env --no-site-packages
source env/bin/activate
pip install grpcio-tools hexdump
python -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. echo.proto
def w(filename):
req = echo_pb2.EchoRequest(firstname='john', lastname='doe')
msg = binascii.b2a_hex(req.SerializeToString())
frame = '00' + hex(len(msg)/2).lstrip("0x").zfill(8) + msg
print 'Raw Encode: ' + frame
f = open(filename, "wb+")
f.write(binascii.a2b_hex(frame))
f.close()
$ xxd frame.bin
00000000: 0a04 6a6f 686e 1203 646f 65 ..john..doe
$ xxd -p frame.bin
0a046a6f686e1203646f65
$ echo `xxd -p frame.bin` | xxd -r -p | protoc --decode_raw
1: "john"
2: "doe"
>>> msg = '0a046a6f686e1203646f65'
>>> print '00' + hex(len(msg)/2).lstrip("0x").zfill(8) + msg
000000000b0a046a6f686e1203646f65
echo -n '000000000b0a046a6f686e1203646f65' | xxd -r -p - frame.bin
Delimited-Message → Compressed-Flag Message-Length Message
Compressed-Flag → 0 / 1 # encoded as 1 byte unsigned integer
Message-Length → {length of Message} # encoded as 4 byte unsigned integer
Message → *{binary octet}
compression:
00
message-length =>11(decimal) octets =>b(hex)
0000000b
msg:
curl -v -k --raw -X POST --http2 \
-H "Content-Type: application/grpc" \
-H "TE: trailers" \
--data-binary @frame.bin \
https://main.esodemoapp2.com:50051/echo.EchoServer/SayHello \
-o resp.bin
nghttp -v -H ":method: POST" \
-H "Content-Type: application/grpc" \
$ nslookup main.esodemoapp2.com 8.8.8.8
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
Name: main.esodemoapp2.com
Address: 127.0.0.1
$ openssl x509 -in server_crt.pem -text -noout
...
Subject: C=US, ST=California, O=Google, OU=Enterprise, CN=main.esodemoapp2.com
$ echo 0a1048656c6c6f2c206a6f686e20646f6521 | xxd -r -p | protoc --decode_raw
1: "Hello, john doe!"
$ python message_util.py read resp.bin
Got wire_message: 00000000120a1048656c6c6f2c206a6f686e20646f6521
Proto Decode: Hello, john doe!
def r(filename):
f = open(filename, "rb")
wire_msg = binascii.b2a_hex(f.read())
f.close()
print 'Got wire_message: ' + wire_msg
xxd resp.bin
00000000: 0000 0000 1f0a 1d53 7472 6561 6d69 6e67 .......Streaming
00000010: 2048 656c 6c6f 2031 202c 206a 6f68 6e20 Hello 1 , john
00000020: 646f 6521 0000 0000 1e0a 1c53 7472 6561 doe!.......Strea
00000030: 6d69 6e67 2048 656c 6c6f 2032 2c20 6a6f ming Hello 2, jo
00000040: 686e 2064 6f65 21 hn doe!
$ xxd -p resp.bin
000000001f
0a1d53747265616d696e672048656c6c6f2031202c206a6f686e20646f6521