source: doc/listings/client/one_off_client.tac

Last change on this file was 170:bb143a4ae70d, checked in by Ralph Meijer <ralphm@…>, 10 years ago

Add documentation using Sphinx and pydoctor, automate doc generation.

File size: 1.4 KB
Line 
1"""
2A one-off XMPP client.
3"""
4
5from twisted.application import service
6from twisted.internet import reactor
7from twisted.python import log
8from twisted.words.protocols.jabber.jid import JID
9from twisted.words.protocols.jabber.xmlstream import IQ
10
11from wokkel import client
12
13NS_VERSION = 'jabber:iq:version'
14
15def getVersion(xmlstream, target):
16    def cb(result):
17        version = {}
18        for element in result.query.elements():
19            if (element.uri == NS_VERSION and
20                element.name in ('name', 'version')):
21                version[element.name] = unicode(element)
22        return version
23
24    iq = IQ(xmlstream, 'get')
25    iq.addElement((NS_VERSION, 'query'))
26    d = iq.send(target.full())
27    d.addCallback(cb)
28    return d
29
30def printVersion(version):
31    print "Name: %s" % version['name'].encode('utf-8')
32    print "Version: %s" % version['version'].encode('utf-8')
33
34jid = JID("user@example.org")
35password = 'secret'
36
37application = service.Application('XMPP client')
38
39factory = client.DeferredClientFactory(jid, password)
40factory.streamManager.logTraffic = True
41
42d = client.clientCreator(factory)
43d.addCallback(lambda _: getVersion(factory.streamManager.xmlstream,
44                                   JID(jid.host)))
45d.addCallback(printVersion)
46d.addCallback(lambda _: factory.streamManager.xmlstream.sendFooter())
47d.addErrback(log.err)
48d.addBoth(lambda _: reactor.callLater(1, reactor.stop))
Note: See TracBrowser for help on using the repository browser.