Skip to content

Instantly share code, notes, and snippets.

@xiantail
Last active December 28, 2015 11:04
Show Gist options
  • Save xiantail/84b53a88d59030c0e26d to your computer and use it in GitHub Desktop.
Save xiantail/84b53a88d59030c0e26d to your computer and use it in GitHub Desktop.
Introducing Python / Chap.11 Exercise 11.5
import zmq
import string
import time
host = '127.0.0.1'
port = 6789
ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
pub.bind('tcp://%s:%s' % (host, port))
with open('mammoth.txt', 'rt') as poem:
words = poem.read()
for word in words.split():
word = word.strip(string.punctuation)
cword = word.encode('utf-8')
if word.startswith(('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') ):
pub.send_multipart([b'vowels', cword])
elif len(word) == 5:
pub.send_multipart([b'five_chars', cword])
else:
#pub.send_multipart([b'others', cword])
pass
import zmq
import string
from time import sleep
host = '127.0.0.1'
port = 6789
ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
pub.bind('tcp://%s:%s' % (host, port))
sleep(1)
with open('mammoth.txt', 'rt') as poem:
words = poem.read()
for word in words.split():
word = word.strip(string.punctuation)
cword = word.encode('utf-8')
if word.startswith(('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') ):
pub.send_multipart([b'vowels', cword])
elif len(word) == 5:
pub.send_multipart([b'five_chars', cword])
else:
#pub.send_multipart([b'others', cword])
pass
import zmq
host = '127.0.0.1'
port = 6789
ctx = zmq.Context()
sub = ctx.socket(zmq.SUB)
sub.connect('tcp://%s:%s' % (host, port))
sub.setsockopt(zmq.SUBSCRIBE, b'vowels')
sub.setsockopt(zmq.SUBSCRIBE, b'five_chars')
sub.setsockopt(zmq.SUBSCRIBE, b'others')
while True:
topic, word = sub.recv_multipart()
print(topic, word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment