我正在开发一个用户必须先登录的应用程序。我正在使用QWebEngineView显示登录页面。但是在其中一台机器上,该页面没有显示。我想知道是否有任何SSL错误。如何获得sslerrors信号并将其连接到插槽。下面的示例代码

from PyQt5.QtNetwork import QSslConfiguration, QSsl
from PyQt5.QtWidgets import QWidget, QGridLayout, QStatusBar, QApplication,\
    QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtCore import QCoreApplication, QMetaObject, QUrl

def set_ssl_protocol():
    default_config = QSslConfiguration.defaultConfiguration()
    default_config.setProtocol(QSsl.TlsV1_2)
    QSslConfiguration.setDefaultConfiguration(default_config)

set_ssl_protocol()

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

        self.urlString = "https://www.yahoo.com"

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.webView = QWebEngineView(self.centralwidget)
        self.webView=QWebEngineView()
        self.webView.setUrl(QUrl("about:blank"))
#        self.webView.setUrl(QUrl("http://www.google.com/"))
        self.webView.setObjectName("webView")
        self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.loadURL()
        self.retranslateUi(MainWindow)
        QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

    def loadURL(self):
        if not hasattr(self, 'page'):
            self.page = QWebEnginePage()
            self.webView.setPage(self.page)
        self.page.load(QUrl(self.urlString))


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


注意:在Qt4中,这很容易,因为我们可以将page()。networkAccessManager()。sslerrors信号连接到插槽,而在Qt5中,由于不是通过NetworkAccessManager发出请求,因此调试起来有点困难。

最佳答案

如果要通过覆盖certificateError()QWebEnginePage方法来获取有关SSL证书导致的错误的信息,如下所示:

from PyQt5 import QtCore, QtWidgets, QtNetwork, QtWebEngineWidgets


def set_ssl_protocol():
    default_config = QtNetwork.QSslConfiguration.defaultConfiguration()
    default_config.setProtocol(QtNetwork.QSsl.TlsV1_2)
    QtNetwork.QSslConfiguration.setDefaultConfiguration(default_config)


class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
    def certificateError(self, certificateError):
        print(certificateError.errorDescription(), certificateError.url(), certificateError.isOverridable())
        error = certificateError.error()
        if error == QtWebEngineWidgets.WebEngineCertificateError.SslPinnedKeyNotInCertificateChain:
            print("SslPinnedKeyNotInCertificateChain")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateCommonNameInvalid:
            print("CertificateCommonNameInvalid")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateDateInvalid:
            print("CertificateDateInvalid")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateAuthorityInvalid:
            print("CertificateAuthorityInvalid")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateContainsErrors:
            print("CertificateContainsErrors")
        if error == QtWebEngineWidgets.WebEngineCertificateError.CertificateNoRevocationMechanism:
            print("CertificateNoRevocationMechanism")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateUnableToCheckRevocation:
            print("CertificateUnableToCheckRevocation")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateRevoked:
            print("CertificateRevoked")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateInvalid:
            print("CertificateAuthorityInvalid")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateWeakSignatureAlgorithm:
            print("CertificateWeakSignatureAlgorithm")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateNonUniqueName:
            print("CertificateNonUniqueName")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateWeakKey:
            print("CertificateWeakKey")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateNameConstraintViolation:
            print("CertificateNameConstraintViolation")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateValidityTooLong:
            print("CertificateValidityTooLong")
        elif error == QtWebEngineWidgets.WebEngineCertificateError.CertificateTransparencyRequired:
            print("CertificateTransparencyRequired")

        return super(WebEnginePage, self).certificateError(certificateError)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.webView = QtWebEngineWidgets.QWebEngineView()
        self.setCentralWidget(self.webView)
        page = WebEnginePage(self)
        self.webView.setPage(page)
        page.load(QtCore.QUrl("https://www.yahoo.com"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    set_ssl_protocol()
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())


如果要包含或不包含请求,可以分别返回True或False。

10-07 23:30