1 # -----------------------------------------------------
 2 # Python hello world demo xmlrpc-client
 3 # Invoke:  python  hello.py  http://myhost:8080
 4 # -----------------------------------------------------
 5 
 6 import xmlrpclib
 7 import sys
 8 
 9 # The url to xmlBlaster (see the output of xmlBlaster server):
10 server_url = sys.argv[1]
11 
12 
13 # -----------------------------------------------------
14 print "\nTrying to connect to xmlBlaster server on ", server_url, " ...\n"
15 # Create an object to represent our server.
16 server = xmlrpclib.Server(server_url)
17 
18 
19 # -----------------------------------------------------
20 # Login to xmlBlaster using a password file:
21 #qos = "<qos><securityService type='htpasswd' version='1.0'> \
22 #           <![CDATA[ \
23 #             <user>michele</user> \
24 #             <passwd>secret</passwd> \
25 #           ]]> \
26 #        </securityService></qos>";
27 
28 # Login to xmlBlaster using default authentication:
29 qos = "<qos></qos>"
30 
31 sessionId = server.authenticate.login( "ben", "secret", qos, "")
32 print "\nLogin success with on ", server_url, " sessionId=", sessionId
33 
34 
35 # -----------------------------------------------------
36 # Call the server and get its current memory consumption.
37 print "\n*  Trying to access the consumed memory of the xmlBlaster server ..."
38 messages = server.xmlBlaster.get( sessionId, "<key oid='__cmd:?totalMem'></key>", "<qos></qos>")
39 print "   Received ", len(messages), " messages:"
40 for msg in messages:
41    key = msg[0]
42    content = msg[1]     # content is of type xmlrpclib.Binary
43    qos = msg[2]
44    print "      key=", key
45    print "      content=", content.data, " bytes"
46    print "      qos=", qos
47 
48 
49 # -----------------------------------------------------
50 print "\n*  Trying to publish a 'Hello world' message ..."
51 # How do i generate a binary content with python??
52 #content = xmlrpclib.Binary(R"Hello world")
53 #server.xmlBlaster.publish( sessionId, "<key oid='MyMessage'></key>", content.data, "<qos></qos>")
54 
55 content = "Hello world"
56 server.xmlBlaster.publish( sessionId, "<key oid='MyMessage'></key>", content, "<qos></qos>")
57 print "   Publish success for message 'MyMessage' content=", content
58 
59 
60 # -----------------------------------------------------
61 # Call the server and get the hello world message again
62 print "\n*  Trying to access our previous published 'Hello world' message ..."
63 messages = server.xmlBlaster.get( sessionId, "<key oid='MyMessage'></key>", "")
64 print "   Received ", len(messages), " messages:"
65 for msg in messages:
66    key = msg[0]
67    content = msg[1]     # content is of type xmlrpclib.Binary
68    qos = msg[2]
69    print "      key=", key
70    print "      content=", content.data
71    print "      qos=", qos
72 
73 
74 # -----------------------------------------------------
75 # Asynchronous access - not yet implemented
76 # We need to instantiate a Python xmlRpc callback server first
77 #server.xmlBlaster.subscribe( sessionId, "<key oid='__cmd:?totalMem'></key>", "<qos></qos>")
78 
79 
80 # -----------------------------------------------------
81 # Logout from xmlBlaster
82 server.authenticate.logout( sessionId ) #server.authenticate.logout( sessionId, "<qos></qos>" )
83 print "\nLogout done, bye.\n"


syntax highlighted by Code2HTML, v. 0.9.1