2012-06-07 60 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'>""") 

等等