QWebEngineView 和忽略证书错误 [英] QWebEngineView and ignoring cert errors

查看:185
本文介绍了QWebEngineView 和忽略证书错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解它是如何工作的,并且正在努力弄清楚如何使用它.我能找到的唯一例子不是用 Python 编写的,而且显然我不太擅长翻译.

I am trying to understand how this works and am struggling to figure out how to use it. The only examples I can find are not in Python and apparently I'm not that good at translating.

我一直在挖掘大多数这些模块的 help() 结果,但仍然无法弄清楚它们是如何工作的.现在,如果我没看错的话,这应该可以用来在加载页面时忽略证书错误.

I've been digging through the help() results for most of these modules but am still not able to figure out how they work. Now if I read it right this should be able to be used to ignore a certificate error when loading a page.

QWebEngineCertificateError.ignoreCertificateError()

但是当我尝试运行它时,出现以下错误.我很确定我使用它是错误的,但我找不到一个很好的例子来说明它应该如何工作.

But when I try to run this I get the following error. I am pretty sure I am using it wrong but I can't find a good example of how its supposed to work.

TypeError: ignoreCertificateError(self): first argument of unbound method must have type 'QWebEngineCertificateError'

在普通浏览器中,当您遇到像这样的证书错误ERR_CERT_AUTHORITY_INVALID" 时,您可以选择继续.该选项似乎不是 QWebEngineView 的默认功能.我想要做的是实现它或让它自动忽略错误并继续.

In a normal browser when you run into a cert error like this "ERR_CERT_AUTHORITY_INVALID" you can choose to proceed anyway. That option doesn't seem to be a default feature of QWebEngineView. What I am trying to do is implement that or to have it automatically just ignore the error and proceed.

有没有人知道如何做到这一点,你愿意给我一个正确方向的指示吗?我难住了.我试图通过将 Chrome 或 Edge 浏览器嵌入到应用程序中来解决这个问题,但它不会让我在浏览器中输入内容,尽管我可以单击内容并右键单击.

Does anyone out there understand how to do this and are you willing to give me a pointer in the right direction? I'm stumped. I tried to get around the problem by just embedding a Chrome or edge browser into the app but it won't let my type in the browser though I can click on things and right click.

这是打开网站时触发相同错误的示例代码.它不是我需要加载的,而是触发相同错误的站点.

Here is an example code opening a website that triggers the same error. Its not what I need to load but its a site that triggers the same error.

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
import sys

app = QApplication(sys.argv)
webview = QWebEngineView()
webview.load(QUrl("https://www.us.army.mil/"))
webview.show()
sys.exit(app.exec_())

错误截图:

推荐答案

你必须重写QWebEnginePage的certificateError()方法:

You have to override the certificateError() method of QWebEnginePage:

import sys


from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication


from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView


class WebEnginePage(QWebEnginePage):
    def certificateError(self, error):
        # If you want to ignore the certificates of certain pages
        # then do something like
        # if error.url() == QUrl("https://www.us.army.mil/"):
        #     error.ignoreCertificateError()
        #     return True
        # return super().certificateError(error)

        error.ignoreCertificateError()
        return True


def main(args):
    app = QApplication(args)
    webview = QWebEngineView()
    page = WebEnginePage()
    webview.setPage(page)
    webview.load(QUrl("https://www.us.army.mil/"))
    webview.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main(sys.argv)
```

这篇关于QWebEngineView 和忽略证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆