Python TLS握手XMPP

Dan*_*Dan 1 python xml ssl xmpp

我正在尝试使用python连接到XMPP服务器。我有要连接的XML,只是不确定如何执行连接的TLS部分?我可以找到许多HTTPS TLS的示例和XMPP的示例,而不仅仅是如何将两者结合在一起。

有没有人使用TLS在python中获得XMPP连接的示例?如果有帮助,我正在尝试连接到talk.google.com。

Joe*_*and 5

首先,请使用他人现有的XMPP 库,而不要编写自己的XMPP 。已经有很多了。从SleekXMPP开始。

要回答您的问题,请在要执行Start-TLS时调用ssl.wrap_socket。例如:

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'>""")
Run Code Online (Sandbox Code Playgroud)

等等。