Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Do websocket module of PySide2 support ssl?

Do websocket module of PySide2 support ssl?

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 1 Posters 766 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • thamT Offline
    thamT Offline
    tham
    wrote on last edited by tham
    #1

    OS : win10 64bits
    python : 3.7.3(anaconda)
    install by pip : pip install PySide2
    llvm: libclang-release_70-based-windows-mingw_64\libclang

    1. QWebSocketServer do not has the api setSslConfiguration, if you call it, it will throw error message "PySide2.QtWebSockets.QWebSocketServer' object has no attribute 'setSslConfiguration'
    2. Since there do not exist setSslConfiguration, I set the ssl configuration as global settings

    Codes of server:

    import sys
    
    from PySide2.QtCore import QFile, QIODevice, QObject, Slot
    from PySide2.QtGui import QFont
    from PySide2.QtNetwork import QHostAddress, QSsl, QSslCertificate, QSslKey
    from PySide2.QtNetwork import QSslConfiguration, QSslSocket
    from PySide2.QtWebSockets import QWebSocketServer
    
    
    from PySide2.QtWidgets import QApplication, QPushButton
    
    
    def setSSLConfig():
        certFile = QFile("keys/localhost.cert")
        keyFile = QFile("keys/localhost.key")
        if certFile.open(QIODevice.ReadOnly) is False:
            raise Exception("Cannot open file keys/localhost.cert")
        if keyFile.open(QIODevice.ReadOnly) is False:
            raise Exception("Cannot open file keys/localhost.key")
    
        certificate = QSslCertificate(certFile.readAll(), QSsl.Pem)
        sslKey = QSslKey(keyFile.readAll(), QSsl.Rsa, QSsl.Pem)
        certFile.close()
        keyFile.close()
        sslConfiguration = QSslConfiguration()
        sslConfiguration.setPeerVerifyMode(QSslSocket.VerifyNone)
        sslConfiguration.setLocalCertificate(certificate)
        sslConfiguration.setPrivateKey(sslKey)
        sslConfiguration.setProtocol(QSsl.TlsV1SslV3)
        QSslConfiguration.setDefaultConfiguration(sslConfiguration)
    
    
    class sslServer(QObject):
    
        def __init__(self):
            super(sslServer, self).__init__()
    
            self.__server = QWebSocketServer("server", QWebSocketServer.SecureMode)
    
        def listen(self, address, port):
            if self.__server.isListening() and self.__addrEqual(address) and self.__portEqual(port):
                return True
            if self.__server.listen(address, port):
                print("server connect to address:", address, "port:", port)
                self.__server.newConnection.connect(self.__clientConnected)
                return True
            else:
                print("Cannot connect to server:", address, ", port:", port)
    
            return False
    
        @Slot(int)
        def listenToIPV4(self, port):
            self.listen(QHostAddress.AnyIPv4, port)
    
        def __clientConnected(self):
            print("client connect to the server")
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    
        setSSLConfig()
    
        server = sslServer()
        server.listenToIPV4(1234)
    
        button = QPushButton(text="Press me if you want to close the server")
        button.resize(640, 480)
        button.setFont(QFont("Arial", 20, QFont.Bold))
        button.clicked.connect(button.close)
        button.show()
    
        sys.exit(app.exec_())
    

    Codes of client

    import sys
    
    from PySide2.QtCore import QObject, QTimer, Slot
    from PySide2.QtGui import QFont
    from PySide2.QtNetwork import QAbstractSocket, QSslConfiguration, QSslSocket
    from PySide2.QtWebSockets import QWebSocket
    from PySide2.QtWidgets import QApplication, QPushButton
    
    
    class sslClient(QObject):
    
        def __init__(self):
            super(sslClient, self).__init__()
    
            self.__socket = QWebSocket()
            self.__socket.connected.connect(self.__connected)
            self.__socket.error.connect(self.__error)
            self.__socket.sslErrors.connect(self.__sslError)
    
            self.__socket.open("wss://localhost:1234")
    
            self.__reconnect_timer = QTimer()
            self.__reconnect_timer.timeout.connect(self.__reconnect)
            self.__reconnect_timer.start(60000)
    
        @Slot()
        def __connected(self):
            print("ssl client connected to server")
    
        @Slot(QAbstractSocket.SocketError)
        def __error(self, ecode):
            print("ssl client error code:", ecode, ", error msg:", self.__socket.errorString())
    
        @Slot(list)
        def __sslError(self, errors):
            print("error:", errors)
    
        def __reconnect(self):
            if self.__socket.state() != QAbstractSocket.ConnectedState:
                print("mlClient reconnect to:", self.__socket.requestUrl())
                self.__socket.open(self.__socket.requestUrl())
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    
        sslConfigure = QSslConfiguration.defaultConfiguration()
        sslConfigure.setPeerVerifyMode(QSslSocket.VerifyNone)
        QSslConfiguration.setDefaultConfiguration(sslConfigure)
    
        client = sslClient()
    
        button = QPushButton(text="Press me if you want to close the client")
        button.resize(640, 480)
        button.setFont(QFont("Arial", 20, QFont.Bold))
        button.clicked.connect(button.close)
        button.show()
    
        sys.exit(app.exec_())
    

    Similar codes works fine with c++(Qt5.12.2), any error I commit?Or ssl do not support by QWebSocket module yet?Or this is a bug? Thanks

    1 Reply Last reply
    0
    • thamT Offline
      thamT Offline
      tham
      wrote on last edited by tham
      #2

      I use asyncio, asyncqt and websockets to replace QWebsocket before Qt for python become more mature

      Edit : This seems like a known issue--Missing ssl support for QWebSocketServer and QWebSocket
      Edit : Check the source codes of PySide2, I am sure the websocket of Qt do not support ssl yet.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved