File: reqhandler.py

package info (click to toggle)
pushpin 1.41.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,056 kB
  • sloc: cpp: 44,382; python: 1,305; sh: 139; makefile: 73; javascript: 34; php: 27
file content (23 lines) | stat: -rw-r--r-- 543 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# this handler responds to every request with "hello world"

import tnetstring
import zmq

ctx = zmq.Context()
sock = ctx.socket(zmq.REP)
sock.connect("ipc://client")

while True:
    m_raw = sock.recv()
    req = tnetstring.loads(m_raw[1:])
    print("IN {}".format(req))

    resp = {}
    resp[b"id"] = req[b"id"]
    resp[b"code"] = 200
    resp[b"reason"] = b"OK"
    resp[b"headers"] = [[b"Content-Type", b"text/plain"]]
    resp[b"body"] = b"hello world\n"

    print("OUT {}".format(resp))
    sock.send(b"T" + tnetstring.dumps(resp))