2012-06-07 59 views
1

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

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

回答

4

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

要回答你的问题,当你想要做Start-TLS时请致电​​。例如:

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'>""") 

等等