Skip to content

Instantly share code, notes, and snippets.

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
import ssl
from hyper import HTTPConnection
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.load_verify_locations(cafile="CA_crt.pem")
context.set_alpn_protocols(['http/1.1', "spdy/1", 'spdy/2', "spdy/3"])
conn = HTTPConnection('main.esodemoapp2.com', 50051, ssl_context=context, secure=True)
params = binascii.a2b_hex(frame)
headers = {"Content-type": "application/grpc", "TE": "trailers"}
conn.request(method="POST", url="/echo.EchoServer/SayHello", body=params, headers=headers)
resp = conn.get_response()
docker run \
--net=host \
--add-host main.esodemoapp2.com:127.0.0.1 \
-v `pwd`:/tmp/ \
salrashid123/grpc_curl \
curl -v -k --raw -X POST \
--http2 \
-H "Content-Type: application/grpc" \
-H "TE: trailers" \
--data-binary @/tmp/frame.bin \
docker run -v `pwd`:/tmp/gcurl/ \
--add-host main.esodemoapp2.com:127.0.0.1 \
--net=host salrashid123/grpc_curl \
curl -v -k --raw -X POST --http2 \
-H "Content-Type: application/grpc" \
-H "TE: trailers" \
--data-binary @/tmp/gcurl/frame.bin \
https://main.esodemoapp2.com:50051/echo.EchoServer/SayHello -o /tmp/gcurl/resp.bin
#!/usr/bin/python
from flask import Flask
from flask import Response
import logging
import socket
import time
import requests