[zeromq-dev] Noob question - pyzmq & ZMQ_STREAM sockets

Virgil Palanciuc virgilp at adobe.com
Wed Feb 19 10:50:57 CET 2014


Hi all,

I¹m trying to re-create the ³HTTP server² example using PYZMQ.
My zmq version(s) are:
Current libzmq version is 4.0.3
Current  pyzmq version is 14.0.1

My first attempt is listed at the end of email. It kinda¹ works, but I
have two questions:


1. For each connection, it prints a message, "Exception
zmq.error.ZMQError: ZMQError('Bad address') in
<zmq.backend.cython.message.Frame object at 0x10fbc35f0> ignored². Is
there something I did wrong, or is this something that is fixed in a more
recent version of libzmq/pyzmq?
2. How can one handle ³keep-alive² HTTP connections? If I don¹t send the
empty message, no response is sent back; but if I send it, the connection
is immediately closed.

Thanks,
Virgil.


=============================
import zmq

context = zmq.Context()
socket = context.socket(zmq.STREAM)
socket.bind("tcp://*:5555")

while True:
  try:
      #  Wait for next request from client
      clientid, message = socket.recv_multipart();

      print("id: %r" % clientid)
      print("request:",message)

      socket.send_multipart([
                  clientid,
      b"""HTTP/1.0 200 OK
Content-Type: text/plain
Content-Length: 13

Hello, world!
"""
      ], copy=False)

      socket.send_multipart([
        clientid,
        b''
      ], copy=False)

      print "message sent, waiting for new one!"
  except KeyboardInterrupt:
    print "SIGINT received, shutting down server..."
    socket.close()
    context.destroy()
    break;
  except Exception as e:
    print "ERROR: unexpected exception ",e





More information about the zeromq-dev mailing list