Skip to content

Instantly share code, notes, and snippets.

@Mause
Created June 14, 2014 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mause/d613ee3b57c6948cd724 to your computer and use it in GitHub Desktop.
Save Mause/d613ee3b57c6948cd724 to your computer and use it in GitHub Desktop.
A as simple as possible example of a ZMQ ROUTER-to-REQ configuration with curve authentication; ironhouse
import logging
logging.basicConfig(level=logging.DEBUG)
import zmq
import zmq.auth
def main(recreate):
if recreate:
zmq.auth.create_certificates('.curve', 'client')
ctx = zmq.Context.instance()
client = ctx.socket(zmq.REQ)
client.curve_publickey, client.curve_secretkey = (
zmq.auth.load_certificate('.curve/client.key_secret')
)
client.curve_serverkey, _ = zmq.auth.load_certificate('.curve/server.key')
client.connect('tcp://127.0.0.1:5071')
client.send_string('Hello')
if __name__ == '__main__':
import sys
main('recreate' in sys.argv)
import glob
import logging
logging.basicConfig(level=logging.DEBUG)
import zmq
import zmq.auth
from zmq.auth.thread import ThreadAuthenticator as Authenticator
def setup_auth(ctx):
# Start an authenticator for this context.
auth = Authenticator(ctx, log=logging.getLogger('zmq.auth'))
auth.start()
auth.allow('127.0.0.1')
# Tell the authenticator how to handle CURVE requests
assert glob.glob('.curve/*.key')
auth.configure_curve(domain='*', location='.curve')
return auth
def setup_server():
ctx = zmq.Context.instance()
# word of warning; if auth ever gets garbage collected, the authenticator
# will get shut down, and authentication will obviously cease to function
auth = setup_auth(ctx)
server = ctx.socket(zmq.ROUTER)
server.curve_publickey, server.curve_secretkey = zmq.auth.load_certificate(
'.curve/server.key_secret'
)
server.curve_server = True
server.bind('tcp://127.0.0.1:5071')
return auth, server
def main(recreate):
if recreate:
zmq.auth.create_certificates('.curve', 'server')
auth, server = setup_server()
while True:
ident, _, message = server.recv_multipart()
print(message)
server.send_string('Hi!')
if __name__ == '__main__':
import sys
main('recreate' in sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment