Python TLS握手XMPP

2024-04-25 10:07:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python连接到XMPP服务器。我有XML要连接,只是不知道如何完成连接的TLS部分?我可以找到很多HTTPS TLS和XMPP的例子,只是不知道如何将两者结合起来。在

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


Tags: https服务器comgoogletlsxmppxml例子
1条回答
网友
1楼 · 发布于 2024-04-25 10:07:52

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

要回答您的问题,请在您想启动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'>""")

等等

相关问题 更多 >