libxml2dom

tests/xmpp_test.py

335:ffaf027ed471
2008-08-28 Paul Boddie Improved error reporting, adding messages for non-validation errors.
     1 #!/usr/bin/env python     2      3 import libxml2dom.xmpp     4 import sys     5      6 sender = "sender" in sys.argv     7 receiver = "receiver" in sys.argv     8 if not (sender or receiver):     9     print "Please specify sender or receiver."    10     sys.exit(1)    11     12 if len(sys.argv) > 2:    13     peer = sys.argv[2]    14 elif sender:    15     peer = "paulb@jeremy/receiver"    16     17 s = libxml2dom.xmpp.Session(("localhost", 5222))    18 d = s.connect("jeremy")    19 print "---- 1 ----"    20 print d.toString()    21     22 auth = s.createAuth()    23 auth.mechanism = "PLAIN"    24 auth.setCredentials("paulb@jeremy", "paulb", "jabber")    25 d = s.send(auth)    26 print "---- 2 ----"    27 print d.toString()    28     29 d = s.connect("jeremy")    30 print "---- 3 ----"    31 print d.toString()    32     33 iq = s.createIq()    34 iq.makeBind()    35 if sender:    36     iq.bind.resource = "sender"    37 else:    38     iq.bind.resource = "receiver"    39 d = s.send(iq)    40 print "---- 4 ----"    41 print d.toString()    42     43 iq = s.createIq()    44 iq.makeSession("jeremy")    45 d = s.send(iq)    46 print "---- 5 ----"    47 print d.toString()    48     49 if sender:    50     message = s.createMessage()    51     message.from_ = "paulb@jeremy/sender"    52     message.to = peer    53     message.type = "chat"    54     message.body = message.createBody()    55     text = message.ownerDocument.createTextNode("Hello!")    56     message.body.appendChild(text)    57     print "Sending..."    58     print message.toString()    59     d = s.send(message)    60     61 if receiver:    62     while 1:    63         print "Listening..."    64         doc = s.receive()    65         print doc.toString()    66         print    67         print "From:", doc.from_    68         print "To:", doc.to    69         print "Type:", doc.type    70         print    71         if doc.localName == "message":    72             print "Message..."    73             if doc.type == "chat" and doc.body:    74                 print doc.body.textContent    75             elif doc.event:    76                 print "Composing?", doc.event.composing    77                 print "Delivered?", doc.event.delivered    78                 print "Displayed?", doc.event.displayed    79                 print "Offline?", doc.event.offline    80                 print "Id:", doc.event.id    81         elif doc.localName == "presence":    82             print "Presence..."    83             if doc.type == "subscribe":    84                 presence = s.createPresence()    85                 presence.type = "subscribed"    86                 presence.from_ = doc.to    87                 presence.to = doc.from_    88                 print "Sending..."    89                 print presence.toString()    90                 d = s.send(presence)    91         print    92     93 # vim: tabstop=4 expandtab shiftwidth=4