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

QNetworkAccessManager https UnknownNetworkError when using pyside2



  • Hello, I’m using the QNetworkAccessManager module and it works fine with HTTP requests, but when I try to do https requests it fails and returns “PySide2.QtNetwork.QNetworkReply.NetworkError.UnknownNetworkError: TLS initialization failed”
    this problem only happens with pyside2 on windows.
    but it works fine on Mac os.
    when I used the same piece of code with pyqt5 it worked just fine on both Windows and Mac os.
    https requests:
    1.pyside2 on windows: fails
    2.pyside2 on mac: works fine
    3. pyqt5 on windows: works fine
    4. pyqt5 on mac: works fine
    and I’m stuck with it for a while and can’t seem to find the problem.

    from PySide2 import QtNetwork
    from PySide2 import QtCore
    import sys, json
    
    class Example:
    
    def __init__(self):
        self.nam = QtNetwork.QNetworkAccessManager()
        self.nam.finished.connect(self.handle_response)
    
    def do_request(self):
        url = 'https://httpbin.org/get'
        req = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
        self.nam.get(req)
    
    def handle_response(self, reply):
        er = reply.error()
        if er == QtNetwork.QNetworkReply.NoError:
            bytes_string = reply.readAll()
            print(str(bytes_string, 'utf-8'))
        else:
            print(f'Error occurred: {er}: {reply.errorString()}')
        QtCore.QCoreApplication.quit()
    def main():
    app = QtCore.QCoreApplication([])
    ex = Example()
    ex.do_request()
    sys.exit(app.exec_())
    
    if name == ‘main’:
    main()
    

    I’m using python 3.7 and pyside2 5.15


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you also have OpenSSL available on your machine ?



  • Yes, and when I tried to run using the command line, these errors popup.
    0b66cde7-49e2-4c38-8e67-38e9248e35fa-image.png
    0dd2c7b5-fdee-41fa-8cb9-9b632492a840-image.png
    17efd56f-0ab1-4aa6-8b80-f4df9155c5b0-image.png

    and I configured the environment variable to point to
    located on C:\Program Files\OpenSSL-Win64\bin
    and I'm not sure why It is searching the C:\WINDOWS path

    32f4dbbc-fff8-4cdb-826d-055b94421421-image.png

    a3a95442-3d9d-48a3-8d95-c3c99b71fb06-image.png


  • Lifetime Qt Champion

    Because it's higher in the list.

    But in any case, having OpenSSL in the Windows folder is usually a bad sign. That's typically a library that shall be provided along an application and not forced there.



  • I looked into the pyside2 package installed on my machine and it seems that it doesn't come with its own libssl-1_1-x64.dll unlike pyqt5 package which comes with its own version.

    I don't know what version that pyside2 requires, and how to fix this ?!!


  • Lifetime Qt Champion

    The Qt Online Installer has an option to download a suitable version of OpenSSL.

    How are you installing PySide2 ?



  • I installed it using pip
    pip install pyside2

    I'm on windows using python 3.7