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. How to use QWebSocket in SecureMode?
Forum Updated to NodeBB v4.3 + New Features

How to use QWebSocket in SecureMode?

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2qt for python
7 Posts 4 Posters 1.3k Views 2 Watching
  • 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.
  • Z Offline
    Z Offline
    zgjja
    wrote on 30 Jun 2020, 07:50 last edited by
    #1

    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_()
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Jun 2020, 19:01 last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Z 1 Reply Last reply 1 Jul 2020, 12:29
      0
      • S SGaist
        30 Jun 2020, 19:01

        Hi and welcome to devnet,

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

        Z Offline
        Z Offline
        zgjja
        wrote on 1 Jul 2020, 12:29 last edited by
        #3

        @SGaist
        PySide2 Version 5.15.0
        OS Version Windows10--19041.330

        P 1 Reply Last reply 1 Jul 2020, 19:51
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 1 Jul 2020, 18:16 last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Z zgjja
            1 Jul 2020, 12:29

            @SGaist
            PySide2 Version 5.15.0
            OS Version Windows10--19041.330

            P Offline
            P Offline
            Pablo J. Rogina
            wrote on 1 Jul 2020, 19:51 last edited by
            #5

            @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...

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            Z 1 Reply Last reply 2 Jul 2020, 08:12
            0
            • P Pablo J. Rogina
              1 Jul 2020, 19:51

              @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...

              Z Offline
              Z Offline
              zgjja
              wrote on 2 Jul 2020, 08:12 last edited by
              #6

              @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.

              J 1 Reply Last reply 2 Jul 2020, 18:13
              0
              • Z zgjja
                2 Jul 2020, 08:12

                @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.

                J Offline
                J Offline
                JonB
                wrote on 2 Jul 2020, 18:13 last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                1/7

                30 Jun 2020, 07:50

                • Login

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