0

hi i am building a desktop application in PyQt python and have a web browser loaded in that , now i want to add functionalities of http fox(Firefox plugin) to view the loaded URLs with the request passed and other headers associated with each URL same as in http fox.

I have written the code from showing the loaded URLs but not finding a way to show the other headers on click of each URL. i have heard about Cookie Jar in Qwebview but do not know how to show with each loaded URL.

My code is:-

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])

i want somethinglike-

[![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]
ricky rana
  • 57
  • 6

1 Answers1

2

Is this what you are looking for?

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()
salomonderossi
  • 2,180
  • 14
  • 20
  • thanks for the help, also i want to know that how to get the user-agent going with the request, i tried "print reply.rawHeader("User-Agent")", but return a empty string. – ricky rana May 03 '16 at 03:13
  • will you please help me, i have also raised a question on this http://stackoverflow.com/questions/36995190/how-to-get-user-agent-and-cookies-going-with-a-request-in-qwebview-pyqt-python – ricky rana May 03 '16 at 04:54
  • in the code posted by you, is it possible to see the Request method ("GET" or "POST") and post parameters if any with the other headers? – ricky rana May 05 '16 at 04:02
  • I updated my answer. But I do not know how to get the post parameters – salomonderossi May 05 '16 at 07:00