4
votes

I have a project will load a HTTP page, parse it, and then open other pages based on the data it received from the first page.

Since Qt's QNetworkAccessManager works asyncronusly, it seems I should be able to load more than one page at a time by continuing to make HTTP requests, and then taking care of the response would happen in the order the replies come back and would be handled by the even loop.

I'm a having a few problems figuring out how to do this though:

First, I read somewhere on stackoverflow that you should use only one QNetworkAccess manager. I do not know if that is true.

The problem is that I'm connecting to the finished slot on the single QNetworkAccess manager. If I do more than one request at a time, I don't know what request the finished signal is in response to. I don't know if there is a way to inspect the QNetworkReply object that is passed from the signal to know what reply it is in response to? Or should I actually be using a different QNetworkAccessManager for each request?

Here is an example of how I'm chaining stuff together right now. But I know this won't work when I'm doing more than one request at at time:

from PyQt4 import QtCore,QtGui,QtNetwork

class Example(QtCore.QObject):
    def __init__(self):
        super().__init__()
        self.QNetworkAccessManager_1 = QtNetwork.QNetworkAccessManager()
        self.QNetworkCookieJar_1 = QtNetwork.QNetworkCookieJar()
        self.QNetworkAccessManager_1.setCookieJar(self.QNetworkCookieJar_1)
        self.app = QtGui.QApplication([])
    def start_request(self):
        QUrl_1 = QtCore.QUrl('https://erikbandersen.com/')
        QNetworkRequest_1 = QtNetwork.QNetworkRequest(QUrl_1)
        #
        self.QNetworkAccessManager_1.finished.connect(self.someurl_finshed)
        self.QNetworkAccessManager_1.get(QNetworkRequest_1)
    def someurl_finshed(self, NetworkReply):
        # I do this so that this function won't get called for a diffent request
        # But it will only work if I'm doing one request at a time
        self.QNetworkAccessManager_1.finished.disconnect(self.someurl_finshed)
        page = bytes(NetworkReply.readAll())
        # Do something with it
        print(page)
        QUrl_1 = QtCore.QUrl('https://erikbandersen.com/ipv6/')
        QNetworkRequest_1 = QtNetwork.QNetworkRequest(QUrl_1)
        #
        self.QNetworkAccessManager_1.finished.connect(self.someurl2_finshed)
        self.QNetworkAccessManager_1.get(QNetworkRequest_1)
    def someurl2_finshed(self, NetworkReply):
        page = bytes(NetworkReply.readAll())
        # Do something with it
        print(page)


kls = Example()
kls.start_request()
1

1 Answers

6
votes

I am not familiar to PyQt but from general Qt programming point of view

  • Using only one QNetworkAccessManager is right design choice
  • finished signal provides QNetworkReply*, with that we can identify corresponding request using request().

I hope this will solve your problem with one manager and multiple requests.

This is a C++ example doing the same.