PDA

View Full Version : Subclassing QNetworkAccessManager in order to save HTTP resources



a_developer
21st December 2015, 18:41
Hello guys,
I'll get straight to the point:
In my code (PyQt), I'm Trying to subclass QNetworkAccessManager so that I could get the data in QNetworkReply, without affecting the contents of the reply.
I do this by connecting myself to readyRead, once readyRead is called I peek the size of bytesAvailable and append that to my own attribute containing the data.
When data is unsupported, it is not read and so the buffer is not cleared, I notice that and change the append to an add.


if reply.isReadable() and reply.isOpen() and reply.bytesAvailable():
reply_data = b''

bytes_available = reply.bytesAvailable()
while len(reply_data) < bytes_available:
bytes_to_read = bytes_available - len(reply_data)
reply_data += bytes(reply.peek(bytes_to_read))

if hasattr(reply, 'is_unsupported') and reply.is_unsupported:
reply.data = reply_data
else:
reply.data += reply_data

However, this still doesn't cover all of the times where the buffer is not cleared, and so at certain times reply.data will appear to hold duplicated data, since it is appended instead of instantiated.

Any helpful insights?

ChrisW67
21st December 2015, 19:41
What are you trying to achieve from the reply user's point of view? Why can't the reply user achieve this for themselves?

a_developer
22nd December 2015, 08:34
Hey Chris,
There is no reply user, please explain your question more thoroughly if you can.

If it helps, I created a similar question on SO
Similar question (http://stackoverflow.com/questions/34405133/python-pyqt5-subclassing-qnetworkaccessmanagercreaterequest-data-duplication)

Perhaps the explanation there is better.