Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QNAM slow upload compared to browser
Wallboy last edited by Wallboy
I'm having an issue with a multipart/form-data upload being much slower than when I make the exact same upload in my browser to my web server. If I use a test form on my Firefox browser, I can max out my upload connection of 20Mbit. However when using QNAM, I am only getting 6.5 Mbit/s with the exact same upload. Here is my test program in PyQt 5.7:
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply, QHttpMultiPart, QHttpPart from PyQt5.QtCore import QUrl, QFile, QByteArray, QIODevice from PyQt5.QtWidgets import QApplication, QMainWindow import sys import ui_test class Upload(QMainWindow, ui_test.Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.pushButton.clicked.connect(self.doUpload) self.manager = QNetworkAccessManager() self.url = QUrl('http://MyWebServer.com/post.php') def doUpload(self): multiPart = QHttpMultiPart(QHttpMultiPart.FormDataType) filePart = QHttpPart() filePart.setHeader(QNetworkRequest.ContentDispositionHeader, 'form-data; name="upload_file"; filename="testfile.mp4"') filePart.setHeader(QNetworkRequest.ContentTypeHeader, 'video/mp4') videoFile = QFile('testfile.mp4') videoFile.open(QIODevice.ReadOnly) # fileInBytes = videoFile.readAll() filePart.setBodyDevice(videoFile) # Maximum ~6.5 Megabit/s # filePart.setBody(fileInBytes) # Maximum ~6.5 Megabit/s videoFile.setParent(multiPart) multiPart.append(filePart) request = QNetworkRequest(self.url) request.setRawHeader(b'User-Agent', b'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0') request.setRawHeader(b'Accept', b'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8') request.setRawHeader(b'Accept-Language', b'en-US,en;q=0.5') request.setRawHeader(b'Accept-Encoding', b'gzip, deflate') request.setRawHeader(b'Connection', b'keep-alive') request.setRawHeader(b'Upgrade-Insecure-Requests', b'1') self.reply = self.manager.post(request, multiPart) # self.reply = self.manager.post(request, fileInBytes) # Maximum ~6.5 Megabit/s multiPart.setParent(self.reply) self.reply.finished.connect(self.finishedUpload) self.reply.uploadProgress.connect(self.slotUploadProgress) def finishedUpload(self): self.reply.deleteLater() self.reply = None def slotUploadProgress(self, bytesSent, bytesTotal): sent = str(bytesSent) total = str(bytesTotal) print(sent + ' bytes of ' + total) if __name__ == '__main__': app = QApplication(sys.argv) main_window = Upload() main_window.show() sys.exit(app.exec_())
I have tried 3 different ways of uploading the data:
- using setBodyDevice() to stream the file upload. Result is 6.5 Mbit/s
- using setBody() and read the entire file first into memory. Result is again 6.5 Mbit/s
- Skipping QHttp*/multipart all-together and using manager.post(request, fileInBytes). Result is again 6.5 Mbit/s
I have also matched the request headers that my browser would normally send to rule out anything there. At this point I am out of ideas on why it's so much slower than when uploading through my browser.
EDIT: I have tried some other test POST servers and I'm getting faster speeds, so I think the issue is likely on my server. But I would still like to know what is going on that would cause the POST to be fast on my browser, but not in my QNAM application?
Any help would be appreciated. Thanks.