Python TLS handshake XMPP
Question: 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.


Answer: First off, use someone else's existing XMPP [library](http://xmpp.org/xmpp-
software/libraries/) instead of writing your own, please. There are plenty
already. Start with [SleekXMPP](https://github.com/fritzy/SleekXMPP).

To answer your question, call
[ssl.wrap_socket](http://docs.python.org/library/ssl.html#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.

