在 QWebview 浏览器 Pyqt Python 中显示 headers

Showing headers in QWebview Browser Pyqt Python

你好,我正在 PyQt 中构建一个桌面应用程序 python 并在其中加载了一个网络浏览器,现在我想添加 http fox(Firefox 插件)的功能来查看加载的 URL s 请求已通过,其他 headers 与每个 URL 关联,与 http fox 中相同。

我已经编写了显示加载的 URL 的代码,但没有找到在单击每个 URL 时显示其他 headers 的方法。我听说过 Qwebview 中的 Cookie Jar,但不知道如何在每次加载时显示 URL。

我的代码是:-

class Manager(QNetworkAccessManager):
def __init__(self, table):
    QNetworkAccessManager.__init__(self)
    self.finished.connect(self._finished)
    self.table = table

def _finished(self, reply):
    headers = reply.rawHeaderPairs()
    headers = {str(k):str(v) for k,v in headers}
    content_type = headers.get("Content-Type")
    url = reply.url().toString()
    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    status, ok = status.toInt()
    self.table.update([url, str(status), content_type])

我想要类似的东西-

[![here on the upper part we have loaded URLs and below that we can see the header, i have written the code for loaded URLs but how to show the headers][1]][1]

这是您要找的吗?

import logging
import sys
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest
from PyQt4.QtCore import QUrl, QEventLoop

log = logging.getLogger(__name__)


class Manager(QNetworkAccessManager):
    def __init__(self, table=list()):
        QNetworkAccessManager.__init__(self)
        self.finished.connect(self._finished)
        self.table = table

    def _finished(self, reply):
        headers = reply.rawHeaderPairs()
        headers = {str(k): str(v) for k, v in headers}
        content_type = headers.get("Content-Type")
        url = reply.url().toString()
        status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
        status, ok = status.toInt()
        self.table.append([str(url), str(status), content_type])
        log.info(self.table)
        request = reply.request()
        log.info(request.rawHeader("User-Agent"))
        method = reply.operation()
        if method == QNetworkAccessManager.GetOperation:
            log.info("get")
            request.url().queryItems()
        if method == QNetworkAccessManager.PostOperation:
            log.info("post")

def test():
    manager = Manager()
    log.info("Sending request")
    manager.get(QNetworkRequest(QUrl("http://www.google.com/")))

    # just for testing purpose to wait for the request to finish
    l = QEventLoop()
    manager.finished.connect(l.quit)
    l.exec_()

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)

    app = QApplication(sys.argv)
    test()