在QWebEngineView中获取sslerrors信号
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在QWebEngineView中获取sslerrors信号相关的知识,希望对你有一定的参考价值。
我正在开发一个用户必须先登录的应用程序。我正在使用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。
以上是关于在QWebEngineView中获取sslerrors信号的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Qt QWebEngineView 获取 HTTP 状态码?
使用QWebEngineView显示html时如何获取verticalScrollBar的最大值和当前值