0

嗨,我正在用 PyQt python 构建一个桌面应用程序并在其中加载了一个 Web 浏览器,现在我想添加 http fox(Firefox 插件)的功能,以查看已加载的 URL 以及传递的请求以及与每个 URL 相关联的其他标头与相同在 http 狐狸。

我已经编写了显示加载的 URL 的代码,但没有找到在单击每个 URL 时显示其他标题的方法。我听说过 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]
4

1 回答 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()
于 2016-04-29T11:12:56.483 回答