john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

xmpp connect XMPPClientFactory TLS

import sys
from twisted.words.protocols.jabber import xmlstream, client, jid
from twisted.internet import reactor


def rawDataIn( buf ):
    print "RECV: %s" % unicode( buf , 'utf-8' ).encode( 'ascii' , 'replace' )

def rawDataOut( buf ):
    print "SEND: %s" % unicode( buf , 'utf-8' ).encode( 'ascii' , 'replace' )


def connected( xml_stream ):
    print 'Connected.'
    xml_stream.rawDataInFn = rawDataIn      # override the xmlstream methods to provide the desired logging functionality
    xml_stream.rawDataOutFn = rawDataOut

def disconnected( xml_stream ):     # control + c will disconnect the stream
    print 'Disconnected.'


if __name__ == '__main__':

    if len( sys.argv ) != 3 :
        print "correct usage: python xmppclient.py fulljid@chat.hipchat.com/device password"
        sys.exit( 1 )

    myJid = jid.JID( sys.argv[1] )
    # factory = client.basicClientFactory( myJid, sys.argv[2] )   # returns a factory with an authenticator, but does not support TLS (xmpp-tls)
    factory = client.XMPPClientFactory( myJid , sys.argv[2] )     # returns a factory with an authenticator, uses xmpp-tls

    factory.addBootstrap( xmlstream.STREAM_CONNECTED_EVENT , connected )    # when there's a connected event, use the connected function
    factory.addBootstrap( xmlstream.STREAM_END_EVENT , disconnected )       # when there's a stream end event, use the disconnected function
    reactor.connectTCP( 'chat.hipchat.com' ,5222, factory )           # connects to the host:port, then depends on the factory's callBacks
    reactor.run()       # run until an event is received that has a termination function (or until control + c )

  • « xmpp connect no encryption
  • input reader stdio commands get url »

Published

Jul 19, 2013

Category

python-twisted

~168 words

Tags

  • connect 6
  • python 180
  • tls 1
  • twisted 20
  • xmpp 4
  • xmppclientfactory 1