Search code examples
pythonpyqtqwebviewhttpfox

Showing headers in QWebview Browser Pyqt Python


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]

Solution

  • 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()