def download(self):
        """Downloading the file.

        :returns: True if success, otherwise returns a tuple with format like
            this (QNetworkReply.NetworkError, error_message)

        :raises: IOError - when cannot create output_path
        """
        # Prepare output path
        self.output_file = QFile(self.output_path)
        if not self.output_file.open(QFile.WriteOnly):
            raise IOError(self.output_file.errorString())

        # Prepare downloaded buffer
        self.downloaded_file_buffer = QByteArray()

        # Request the url
        request = QNetworkRequest(self.url)
        self.reply = self.manager.get(request)
        self.reply.readyRead.connect(self.get_buffer)
        self.reply.finished.connect(self.write_data)
        self.manager.requestTimedOut.connect(self.request_timeout)

        if self.progress_dialog:
            # progress bar
            def progress_event(received, total):
                """Update progress.

                :param received: Data received so far.
                :type received: int

                :param total: Total expected data.
                :type total: int
                """
                # noinspection PyArgumentList
                QgsApplication.processEvents()

                self.progress_dialog.adjustSize()

                human_received = humanize_file_size(received)
                human_total = humanize_file_size(total)

                label_text = tr("%s : %s of %s" % (
                    self.prefix_text, human_received, human_total))

                self.progress_dialog.setLabelText(label_text)
                self.progress_dialog.setMaximum(total)
                self.progress_dialog.setValue(received)

            # cancel
            def cancel_action():
                """Cancel download."""
                self.reply.abort()
                self.reply.deleteLater()

            self.reply.downloadProgress.connect(progress_event)
            self.progress_dialog.canceled.connect(cancel_action)

        # Wait until finished
        # On Windows 32bit AND QGIS 2.2, self.reply.isFinished() always
        # returns False even after finished slot is called. So, that's why we
        # are adding self.finished_flag (see #864)
        while not self.reply.isFinished() and not self.finished_flag:
            # noinspection PyArgumentList
            QgsApplication.processEvents()

        result = self.reply.error()
        try:
            http_code = int(self.reply.attribute(
                QNetworkRequest.HttpStatusCodeAttribute))
        except TypeError:
            # If the user cancels the request, the HTTP response will be None.
            http_code = None

        self.reply.abort()
        self.reply.deleteLater()

        if result == QNetworkReply.NoError:
            return True, None

        elif result == QNetworkReply.UnknownNetworkError:
            return False, tr(
                'The network is unreachable. Please check your internet '
                'connection.')

        elif http_code == 408:
            msg = tr(
                'Sorry, the server aborted your request. '
                'Please try a smaller area.')
            LOGGER.debug(msg)
            return False, msg

        elif http_code == 509:
            msg = tr(
                'Sorry, the server is currently busy with another request. '
                'Please try again in a few minutes.')
            LOGGER.debug(msg)
            return False, msg

        elif result == QNetworkReply.ProtocolUnknownError or \
                result == QNetworkReply.HostNotFoundError:
            # See http://doc.qt.io/qt-5/qurl-obsolete.html#encodedHost
            encoded_host = self.url.toAce(self.url.host())
            LOGGER.exception('Host not found : %s' % encoded_host)
            return False, tr(
                'Sorry, the server is unreachable. Please try again later.')

        elif result == QNetworkReply.ContentNotFoundError:
            LOGGER.exception('Path not found : %s' % self.url.path())
            return False, tr('Sorry, the layer was not found on the server.')

        else:
            return result, self.reply.errorString()
def download ( self ) : self . output_file = QFile ( self . output_path ) if not self . output_file . open ( QFile . WriteOnly ) : raise IOError ( self . output_file . errorString ( ) ) self . downloaded_file_buffer = QByteArray ( ) request = QNetworkRequest ( self . url ) self . reply = self . manager . get ( request ) self . reply . readyRead . connect ( self . get_buffer ) self . reply . finished . connect ( self . write_data ) self . manager . requestTimedOut . connect ( self . request_timeout ) if self . progress_dialog : def progress_event ( received , total ) : QgsApplication . processEvents ( ) self . progress_dialog . adjustSize ( ) human_received = humanize_file_size ( received ) human_total = humanize_file_size ( total ) label_text = tr ( "%s : %s of %s" % ( self . prefix_text , human_received , human_total ) ) self . progress_dialog . setLabelText ( label_text ) self . progress_dialog . setMaximum ( total ) self . progress_dialog . setValue ( received ) def cancel_action ( ) : self . reply . abort ( ) self . reply . deleteLater ( ) self . reply . downloadProgress . connect ( progress_event ) self . progress_dialog . canceled . connect ( cancel_action ) while not self . reply . isFinished ( ) and not self . finished_flag : QgsApplication . processEvents ( ) result = self . reply . error ( ) try : http_code = int ( self . reply . attribute ( QNetworkRequest . HttpStatusCodeAttribute ) ) except TypeError : http_code = None self . reply . abort ( ) self . reply . deleteLater ( ) if result == QNetworkReply . NoError : return True , None elif result == QNetworkReply . UnknownNetworkError : return False , tr ( 'The network is unreachable. Please check your internet ' 'connection.' ) elif http_code == 408 : msg = tr ( 'Sorry, the server aborted your request. ' 'Please try a smaller area.' ) LOGGER . debug ( msg ) return False , msg elif http_code == 509 : msg = tr ( 'Sorry, the server is currently busy with another request. ' 'Please try again in a few minutes.' ) LOGGER . debug ( msg ) return False , msg elif result == QNetworkReply . ProtocolUnknownError or result == QNetworkReply . HostNotFoundError : encoded_host = self . url . toAce ( self . url . host ( ) ) LOGGER . exception ( 'Host not found : %s' % encoded_host ) return False , tr ( 'Sorry, the server is unreachable. Please try again later.' ) elif result == QNetworkReply . ContentNotFoundError : LOGGER . exception ( 'Path not found : %s' % self . url . path ( ) ) return False , tr ( 'Sorry, the layer was not found on the server.' ) else : return result , self . reply . errorString ( )
Downloading the file.

        :returns: True if success, otherwise returns a tuple with format like
            this (QNetworkReply.NetworkError, error_message)

        :raises: IOError - when cannot create output_path