Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use QWebSocket in SecureMode?



  • How to use QWebsocket with Securemode(SSL)?
    i'm a amateur and i want to build a simple chatting tool by QWebsocket,but i found my server&client program could communicate with each other well while the server was NonSecuremode,but if i changed it to secure mode,my client can't connect to it.Anybody knows why?Thanks!
    Here is the Server:

    from PySide2.QtWebSockets import *
    from PySide2.QtCore import *
    from PySide2.QtWidgets import *
    
    
    class A(QMainWindow):
        def __init__(self, parent=None):
            super(A, self).__init__(parent)
            self.server = QWebSocketServer('server', QWebSocketServer.SecureMode, parent)
            self.server.listen(QHostAddress.LocalHost, port=6666)
            self.server.newConnection.connect(self.handle)
    
        def handle(self):
            a = self.server.nextPendingConnection()
            print('there is a new client')
    
    app = QApplication()
    b = A()
    b.show()
    
    app.exec_()
    

    Here is the Clinet:

    from PySide2.QtWebSockets import *
    from PySide2.QtCore import *
    from PySide2.QtWidgets import *
    
    
    class A(QMainWindow):
        def __init__(self, parent=None):
            super(A, self).__init__(parent)
            self.a = QWebSocket()
            self.a.open(QUrl('wss://127.0.0.1:6666'))
    
    
    
    app = QApplication()
    b = A()
    b.show()
    print(b.a.state())
    app.exec_()
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of PySide2/PyQt5 are you using ?
    On what OS ?



  • @SGaist
    PySide2 Version 5.15.0
    OS Version Windows10--19041.330


  • Lifetime Qt Champion

    Did you check whether you have the OpenSSL dlls available on your system ?



  • @zgjja you may want to check the SSL Echo Server example where (although in C++) you'll see how the secure server is created.

    BTW, you'll need a server certificate (very likely a self-signed one) and a private key...



  • @Pablo-J-Rogina @SGaist
    I checked the C++ code and i found that the WebSocketServer Class in C++ has a "setsslconfiguration" function while the Python doesn't have.If i construct a SSL certification like this:

            cacert_key = QFile('cacert.pem')
            priv_key = QFile('privkey.pem')
            server_config = QSslConfiguration()
            server_config.setLocalCertificate(cacert_key)
            server_config.setPrivateKey(priv_key)
    

    but i don't know how to implement this to the WebScoketServer i build.



  • This post is deleted!

Log in to reply