I'm trying to create an HTTP server with ZMQ_STREAM
socket.
When I do a simple POST
request:
POST HTTP/1.1
Host: localhost:5555
Cache-Control: no-cache
Postman-Token: 67004be5-56bc-c1a9-847a-7db3195c301d
Apples to Oranges!
Here is how I handle this with pyzmq:
context = zmq.Context()
socket = context.socket(zmq.STREAM)
socket.bind("tcp://*:5555")
while True:
# Get HTTP request
parts = []
id_, msg = socket.recv_multipart() # [id, ''] or [id, http request]
parts.append(id_)
parts.append(msg)
if not msg:
# This is a new connection - this is just the identify frame (throw away id_)
# The body will come next
id_, msg = socket.recv_multipart() # [id, http request]
parts.append(id_)
parts.append(msg)
end = socket.recv_multipart() # [id*, ''] <- some kind of junk?
parts.append(end)
print("%s" % repr(parts))
So that parts
list comes out to be:
['\x00\x80\x00\x00)', '', '\x00\x80\x00\x00)', 'POST / HTTP/1.1\r\nHost: localhost:5555\r\nConnection: keep-alive\r\nContent-Length: 18\r\nCache-Control: no-cache\r\nOrigin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop\r\nContent-Type: text/plain;charset=UTF-8\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36\r\nPostman-Token: 9503fce9-8b1c-b39c-fb4d-3a7f21b509de\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.8,ru;q=0.6,uk;q=0.4\r\n\r\nApples to Oranges!', ['\x00\x80\x00\x00*', '']]
So I understand that:
'\x00\x80\x00\x00)', ''
is the identity of the connection. This is set initially byZMQ_STREAM
socket. On subsequent requests it seems to be absent.\x00\x80\x00\x00)
is the identity again, this is what we see on subsequent requests from the client fromZMQ_STREAM
socket.- Then the actual HTTP request
But the last pair of magic numbers: ['\x00\x80\x00\x00*', '']
What the heck does that stand for?
References: