-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathweather.py
More file actions
31 lines (25 loc) · 900 Bytes
/
weather.py
File metadata and controls
31 lines (25 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#
# Weather update client
# Connects SUB socket to tcp://localhost:5556
# Collects weather updates and finds avg temp in zipcode
# Works in conjunction with C:\Users\nikoulis\Documents\Visual Studio 2010\Projects\zmqBroadcast
#
import sys
import zmq
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)
print "Collecting updates from weather server..."
socket.connect("tcp://localhost:5556")
# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"
socket.setsockopt(zmq.SUBSCRIBE, zip_filter)
while(1):
# Process 5 updates
total_temp = 0
for update_nbr in range(10):
string = socket.recv()
zipcode, temperature, relhumidity = string.split()
total_temp += int(temperature)
print "Average temperature for zipcode '%s' was %dF" % (
zip_filter, total_temp / update_nbr)