Skip to content

Instantly share code, notes, and snippets.

@claws
Created January 12, 2013 23:06
Show Gist options
  • Save claws/4520939 to your computer and use it in GitHub Desktop.
Save claws/4520939 to your computer and use it in GitHub Desktop.
pyzmq problem using LAST_ENDPOINT sockopt
'''
I am trying to use a wildcard port in an effort to let the OS assign
an ephemeral port. I then try to ascertain the actual endpoint by
using the LAST_ENDPOINT socket option.
This is along the lines of the suggestion here: http://lists.zeromq.org/pipermail/zeromq-dev/2012-October/018915.html
However, this does not work for me. The output below is what I get
when I run this script:
Python version: 2.7.2 (default, Jun 20 2012, 16:23:33) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]
zmq version: 3.2.2
pyzmq version: 2.2dev
Endpoint is tcp://192.168.1.2:*
Traceback (most recent call last):
File "zmq_test.py", line 38, in <module>
router.getsockopt(zmq.LAST_ENDPOINT)
File "socket.pyx", line 394, in zmq.core.socket.Socket.getsockopt (zmq/core/socket.c:3915)
zmq.core.error.ZMQError: Invalid argument
I am aware that pyzmq has the bind_to_random_port function but that
does not really fit my need exactly as that could potentially block
for some extended period of time. In most cases it would likely be
very short but I am trying to integrate ZMQ and Twisted together so
I am trying to implement a solution that avoids blocking whereever
possible.
'''
import socket
import sys
import zmq
print "Python version: %s" % ' '.join(sys.version.split('\n'))
print "zmq version: %s" % zmq.zmq_version()
print "pyzmq version: %s" % zmq.pyzmq_version()
host_ip = socket.gethostbyname(socket.gethostname())
endpoint = 'tcp://%s:*' % host_ip
print "Endpoint is %s" % endpoint
ctx = zmq.Context()
router = ctx.socket(zmq.ROUTER)
router.bind(endpoint)
router.getsockopt(zmq.LAST_ENDPOINT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment