Dan
Dan

Reputation: 250

Python TLS handshake XMPP

I'm trying to connect to an XMPP server using python. I have the XML to connect, I'm just not sure how to do the TLS portion of the connection? I can find lots of examples for HTTPS TLS and examples for XMPP just not how to put both together.

Has anyone got an example of an XMPP connection in python using TLS? I'm trying to connect to talk.google.com if that helps.

Upvotes: 1

Views: 1701

Answers (1)

Joe Hildebrand
Joe Hildebrand

Reputation: 10414

First off, use someone else's existing XMPP library instead of writing your own, please. There are plenty already. Start with SleekXMPP.

To answer your question, call ssl.wrap_socket when you want to do Start-TLS. For example:

import socket
import ssl

sock = socket.create_connection(("example.com", 5222))
sock.write("""<stream:stream
                 to='example.com'
                 version='1.0'
                 xml:lang='en'
                 xmlns='jabber:client'
                 xmlns:stream='http://etherx.jabber.org/streams'>""")
sock.recv(1000) # reads the stream:stream and stream:features.  Obviously bad code, to get the point accross
sock.write("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>")
sock.recv(1000) # read the proceed
ssl_sock = ssl.wrap_socket(sock)
ssl_sock.write("""<stream:stream
                 to='example.com'
                 version='1.0'
                 xml:lang='en'
                 xmlns='jabber:client'
                 xmlns:stream='http://etherx.jabber.org/streams'>""")

Etc.

Upvotes: 4

Related Questions