Created
August 28, 2011 16:33
-
-
Save mgunneras/1176877 to your computer and use it in GitHub Desktop.
Publish data from file or stdin to a zmq socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
""" | |
Prints data from zmq PULL socket to stdout | |
""" | |
import zmq | |
import sys | |
def get_socket(host, port): | |
context = zmq.Context() | |
socket = context.socket(zmq.PULL) | |
socket.bind('tcp://%s:%s' % (host, port)) | |
return socket | |
def print_socket(socket, stdout): | |
while 1: | |
chunk = socket.recv() | |
stdout.write(chunk) | |
stdout.flush() | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description='Proxies stdin to PUB socket over zmq') | |
parser.add_argument('--host', type=str, default='0.0.0.0', help='Host to connect on') | |
parser.add_argument('--port', type=int, default=9900, help='Port to connect to') | |
args = parser.parse_args() | |
sock = get_socket(args.host, args.port) | |
try: | |
print_socket(sock, sys.stdout) | |
except KeyboardInterrupt: | |
sys.exit(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
""" | |
Utility to read from file and publish the data over a zmq socket using PUSH. | |
This utility will block until killed | |
""" | |
import zmq | |
import sys | |
VERBOSE = False | |
def get_socket(host, port): | |
context = zmq.Context() | |
socket = context.socket(zmq.PUSH) | |
socket.connect('tcp://%s:%s' % (host, port)) | |
return socket | |
def publish_file(socket, fd, chunk_size=1): | |
while 1: | |
chunk = fd.read(chunk_size) | |
if VERBOSE: | |
sys.stdout.write('DATA: %s\n'% chunk) | |
socket.send(chunk) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description='Reads infile and PUSHes to socket over zmq') | |
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help="file to read. defaults to stdin") | |
parser.add_argument('--host', type=str, default='0.0.0.0', help='Host to listen on') | |
parser.add_argument('--port', type=int, default=9900, help='Port to listen on') | |
parser.add_argument('--verbose', action='store_true', default=False, help='Print stuff') | |
parser.add_argument('--chunksize', type=int, default=1) | |
args = parser.parse_args() | |
VERBOSE = args.verbose | |
sock = get_socket(args.host, args.port) | |
publish_file(sock, args.infile, args.chunksize) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment