pubsubclient: 8469faa08708092f8ae5fe4a7e52606ffc81e523
1: #!/usr/bin/env python
2: import pubsubclient
3: import time
4:
5: # This will run when we get a reply
6: def handle_nodes(node_list):
7: # This function should take one argument, a list of pubsubclient.Node objects
8: global client
9: # We've received a reply, so reduce the amount we're waiting for by one
10: global wait
11: wait -= 1
12:
13: for node in node_list:
14: print "Found node " + str(node) # str on a Node will give its name/ID
15: # Now we can request this Node's children
16: # We need to supply a PubSubClient to use and a return function
17: node.get_sub_nodes(client, handle_nodes)
18: # We're now waiting for another reply, so increment wait
19: wait += 1
20:
21:
22: global wait
23: global client
24: # Make a client
25: client = pubsubclient.PubSubClient("test1@localhost", "test")
26: # Log on
27: client.connect()
28:
29: # The following line requests the top-level nodes on aserver.com and makes handle_nodes the return function
30: client.get_nodes('pubsub.localhost', node=None, return_function=handle_nodes)
31:
32: # This keeps track of how many replies we are waiting on
33: # Needs to be global since we're using it in the function too
34: wait = 1
35: while wait > 0:
36: # Handle any replies
37: client.process()
38: # Wait for a second
39: time.sleep(1)
40: # Now we can close
Generated by git2html.