From 31f56408d7658ba7c6ebc32febf59b6b9b7fc61a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 17 Mar 2012 10:33:29 -0400 Subject: [PATCH] Filling in the python weather update client, too. --- zeromq/wuclient.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 zeromq/wuclient.py diff --git a/zeromq/wuclient.py b/zeromq/wuclient.py new file mode 100644 index 0000000..db20776 --- /dev/null +++ b/zeromq/wuclient.py @@ -0,0 +1,32 @@ +# vim:fileencoding=utf-8 + +from __future__ import print_function + +import sys +import zmq + + +def main(sysargs=sys.argv[:]): + context = zmq.Context() + socket = context.socket(zmq.SUB) + + print("Collecting updates from weather server…") + socket.connect("tcp://localhost:5556") + + target_zipcode = sysargs[1] if len(sysargs) > 1 else '10001' + socket.setsockopt(zmq.SUBSCRIBE, target_zipcode) + + total_temp = 0 + for update_nbr in range(100): + string = socket.recv() + total_temp += int(string.split()[1]) + + print("Average temperature for zipcode '{}' was {}F".format( + target_zipcode, total_temp / update_nbr + )) + + return 0 + + +if __name__ == '__main__': + sys.exit(main())