【问题标题】:Python TLS handshake XMPPPython TLS 握手 XMPP
【发布时间】:2012-06-11 02:50:30
【问题描述】:

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

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

【问题讨论】:

    标签: python xml xmpp ssl


    【解决方案1】:

    首先,请使用其他人现有的 XMPP library,而不是自己编写。已经有很多了。以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'>""")
    

    等等

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2014-12-09
      • 2021-01-02
      • 2017-01-17
      • 1970-01-01
      • 2019-12-16
      相关资源
      最近更新 更多