Class: QgsNetworkAccessManager

class qgis.core.QgsNetworkAccessManager

Bases: PyQt5.QtNetwork.QNetworkAccessManager

network access manager for QGIS

This class implements the QGIS network access manager. It’s a singleton that can be used across QGIS.

Plugins can insert proxy factories and thereby redirect requests to individual proxies.

If no proxy factories are there or none returns a proxy for an URL a fallback proxy can be set. There’s also a exclude list that defines URLs that the fallback proxy should not be used for, then no proxy will be used.

New in version 1.5:

Methods

abortAuthBrowser

Abort any outstanding external browser login request.

blockingGet

Posts a GET request to obtain the contents of the target request and returns a new QgsNetworkReplyContent object for reading.

blockingPost

Posts a POST request to obtain the contents of the target request, using the given data, and returns a new QgsNetworkReplyContent object for reading.

cacheLoadControlFromName

Returns QNetworkRequest.CacheLoadControl from a name.

cacheLoadControlName

Returns the name for QNetworkRequest.CacheLoadControl.

childEvent

connectNotify

createRequest

param op:

customEvent

disconnectNotify

excludeList

Returns the proxy exclude list.

fallbackProxy

Returns the fallback proxy used by the manager.

insertProxyFactory

Inserts a factory into the proxy factories list.

instance

Returns a pointer to the active QgsNetworkAccessManager for the current thread.

isSignalConnected

noProxyList

Returns the no proxy list.

preprocessRequest

Preprocesses request

proxyFactories

Returns a list of proxy factories used by the manager.

receivers

removeProxyFactory

Removes a factory from the proxy factories list.

removeReplyPreprocessor

Removes the custom reply pre-processor function with matching id.

removeRequestPreprocessor

Removes the custom request pre-processor function with matching id.

requestAuthCloseBrowser

Forwards an external browser login closure request to the authentication handler.

requestAuthOpenBrowser

Forwards an external browser login url opening request to the authentication handler.

sender

senderSignalIndex

setFallbackProxyAndExcludes

Sets the fallback proxy and URLs which shouldn't use it.

setReplyPreprocessor

Sets a reply pre-processor function, which allows manipulation of QNetworkReply objects after they are created (but before they are fetched).

setRequestPreprocessor

Sets a request pre-processor function, which allows manipulation of a network request before it is processed.

setTimeout

Sets the maximum timeout time for network requests, in milliseconds.

setupDefaultProxyAndCache

Setup the QgsNetworkAccessManager (NAM) according to the user's settings.

supportedSchemesImplementation

timeout

Returns the network timeout length, in milliseconds.

timerEvent

useSystemProxy

Returns whether the system proxy should be used.

Signals

authBrowserAborted

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

cookiesChanged

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

downloadProgress

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

finished

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

requestAboutToBeCreated

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

requestAuthDetailsAdded

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

requestCreated

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

requestEncounteredSslErrors

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

requestRequiresAuth

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

requestTimedOut

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

abortAuthBrowser(self)

Abort any outstanding external browser login request.

Note

Background threads will listen to aborted browser request signals from the network manager on the main thread.

New in version 3.20.

authBrowserAborted

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

blockingGet(request: QNetworkRequest, authCfg: str = '', forceRefresh: bool = False, feedback: QgsFeedback = None) QgsNetworkReplyContent

Posts a GET request to obtain the contents of the target request and returns a new QgsNetworkReplyContent object for reading. The current thread will be blocked until the request is returned.

This method is safe to call in either the main thread or a worker thread.

If forceRefresh is False then previously cached replies may be used for the request. If it is set to True then a new query is always performed.

If an authCfg has been specified, then that authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.

The optional feedback argument can be used to abort ongoing requests.

The contents of the reply will be returned after the request is completed or an error occurs.

See also

blockingPost()

New in version 3.6.

Parameters:
  • request (QNetworkRequest) –

  • authCfg (str = '') –

  • forceRefresh (bool = False) –

  • feedback (QgsFeedback = None) –

Return type:

QgsNetworkReplyContent

blockingPost(request: QNetworkRequest, data: QByteArray | bytes | bytearray, authCfg: str = '', forceRefresh: bool = False, feedback: QgsFeedback = None) QgsNetworkReplyContent

Posts a POST request to obtain the contents of the target request, using the given data, and returns a new QgsNetworkReplyContent object for reading. The current thread will be blocked until the request is returned.

This method is safe to call in either the main thread or a worker thread.

If forceRefresh is False then previously cached replies may be used for the request. If it is set to True then a new query is always performed.

If an authCfg has been specified, then that authentication configuration required will automatically be applied to request. There is no need to manually apply the authentication to the request prior to calling this method.

The optional feedback argument can be used to abort ongoing requests.

The contents of the reply will be returned after the request is completed or an error occurs.

See also

blockingGet()

New in version 3.6.

Parameters:
  • request (QNetworkRequest) –

  • data (Union[QByteArray) –

  • authCfg (str = '') –

  • forceRefresh (bool = False) –

  • feedback (QgsFeedback = None) –

Return type:

QgsNetworkReplyContent

cacheLoadControlFromName(name: str) QNetworkRequest.CacheLoadControl

Returns QNetworkRequest.CacheLoadControl from a name.

Parameters:

name (str) –

Return type:

QNetworkRequest.CacheLoadControl

cacheLoadControlName(control: QNetworkRequest.CacheLoadControl) str

Returns the name for QNetworkRequest.CacheLoadControl.

Parameters:

control (QNetworkRequest.CacheLoadControl) –

Return type:

str

childEvent(self, QChildEvent)
connectNotify(self, QMetaMethod)
cookiesChanged

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

createRequest(self, op: QNetworkAccessManager.Operation, req: QNetworkRequest, outgoingData: QIODevice = None) QNetworkReply
Parameters:
  • op (QNetworkAccessManager.Operation) –

  • req (QNetworkRequest) –

  • outgoingData (QIODevice = None) –

Return type:

QNetworkReply

customEvent(self, QEvent)
disconnectNotify(self, QMetaMethod)
downloadProgress

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

excludeList(self) List[str]

Returns the proxy exclude list.

This list consists of the beginning of URL strings which will not use the fallback proxy.

See also

noProxyList()

See also

fallbackProxy()

Return type:

List[str]

fallbackProxy(self) QNetworkProxy

Returns the fallback proxy used by the manager.

The fallback proxy is used for URLs which no other proxy factory returned proxies for.

See also

proxyFactories()

Return type:

QNetworkProxy

finished

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

insertProxyFactory(self, factory: QNetworkProxyFactory)

Inserts a factory into the proxy factories list.

Ownership of factory is transferred to the manager.

See also

proxyFactories()

Parameters:

factory (QNetworkProxyFactory) –

instance(connectionType: Qt.ConnectionType = Qt.BlockingQueuedConnection) QgsNetworkAccessManager

Returns a pointer to the active QgsNetworkAccessManager for the current thread.

With the connectionType parameter it is possible to setup the default connection type that is used to handle signals that might require user interaction and therefore need to be handled on the main thread. See in-depth discussion below.

Parameters:

connectionType (Qt.ConnectionType = Qt.BlockingQueuedConnection) – In most cases the default of using a Qt.BlockingQueuedConnection is ok, to make a background thread wait for the main thread to answer such a request is fine and anything else is dangerous. However, in case the request was started on the main thread, one should execute a local event loop in a helper thread and freeze the main thread for the duration of the download. In this case, if an authentication request is sent from the background thread network access manager, the background thread should be blocked, the main thread be woken up, processEvents() executed once, the main thread frozen again and the background thread continued.

Return type:

QgsNetworkAccessManager

isSignalConnected(self, QMetaMethod) bool
noProxyList(self) List[str]

Returns the no proxy list.

This list consists of the beginning of URL strings which will not use any proxy at all

See also

excludeList()

See also

fallbackProxy()

Return type:

List[str]

preprocessRequest(self, req: QNetworkRequest)

Preprocesses request

Parameters:

req (QNetworkRequest) – the request to preprocess

New in version 3.22.

proxyFactories(self) List[QNetworkProxyFactory]

Returns a list of proxy factories used by the manager.

See also

proxyFactories()

Return type:

List[QNetworkProxyFactory]

receivers(self, PYQT_SIGNAL) int
removeProxyFactory(self, factory: QNetworkProxyFactory)

Removes a factory from the proxy factories list.

See also

proxyFactories()

Parameters:

factory (QNetworkProxyFactory) –

removeReplyPreprocessor(id: str)

Removes the custom reply pre-processor function with matching id.

The id must correspond to a pre-processor previously added via a call to setReplyPreprocessor().

Returns True if processor existed and was removed.

New in version 3.26.

Parameters:

id (str) –

removeRequestPreprocessor(id: str)

Removes the custom request pre-processor function with matching id.

The id must correspond to a pre-processor previously added via a call to setRequestPreprocessor().

Returns True if processor existed and was removed.

New in version 3.22.

Parameters:

id (str) –

requestAboutToBeCreated

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

requestAuthCloseBrowser(self)

Forwards an external browser login closure request to the authentication handler.

Note

If called by a background thread, the request will be forwarded to the network manager on the main thread.

New in version 3.20.

requestAuthDetailsAdded

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

requestAuthOpenBrowser(self, url: QUrl)

Forwards an external browser login url opening request to the authentication handler.

Note

If called by a background thread, the request will be forwarded to the network manager on the main thread.

New in version 3.20.

Parameters:

url (QUrl) –

requestCreated

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

requestEncounteredSslErrors

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

requestRequiresAuth

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

requestTimedOut

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

sender(self) QObject
senderSignalIndex(self) int
setFallbackProxyAndExcludes(self, proxy: QNetworkProxy, excludes: Iterable[str], noProxyURLs: Iterable[str])

Sets the fallback proxy and URLs which shouldn’t use it.

The fallback proxy is used for URLs which no other proxy factory returned proxies for. The excludes list specifies the beginning of URL strings which will not use this fallback proxy. The noProxyURLs list specifies the beginning of URL strings which will not use any proxy at all

See also

fallbackProxy()

See also

excludeList()

See also

noProxyList()

Parameters:
  • proxy (QNetworkProxy) –

  • excludes (Iterable[str]) –

  • noProxyURLs (Iterable[str]) –

setReplyPreprocessor(Callable[..., None]) str

Sets a reply pre-processor function, which allows manipulation of QNetworkReply objects after they are created (but before they are fetched).

The processor function takes a QNetworkRequest request and a QNetworkReply as arguments, and can connect to QNetworkReply signals directly as desired.

Return type:

str

Returns:

An auto-generated string uniquely identifying the preprocessor, which can later be used to remove the preprocessor (via a call to removeReplyPreprocessor()).

New in version 3.26.

setRequestPreprocessor(Callable[..., None]) str

Sets a request pre-processor function, which allows manipulation of a network request before it is processed.

The processor function takes the QNetworkRequest as its argument, and can mutate the request if necessary.

Return type:

str

Returns:

An auto-generated string uniquely identifying the preprocessor, which can later be used to remove the preprocessor (via a call to removeRequestPreprocessor()).

New in version 3.22.

setTimeout(time: int)

Sets the maximum timeout time for network requests, in milliseconds. If set to 0, no timeout is set.

See also

timeout()

New in version 3.6.

Parameters:

time (int) –

setupDefaultProxyAndCache(self, connectionType: Qt.ConnectionType = Qt.BlockingQueuedConnection)

Setup the QgsNetworkAccessManager (NAM) according to the user’s settings. The connectionType sets up the default connection type that is used to handle signals that might require user interaction and therefore need to be handled on the main thread. See in-depth discussion in the documentation for the constructor of this class.

Parameters:

connectionType (Qt.ConnectionType = Qt.BlockingQueuedConnection) –

supportedSchemesImplementation(self) List[str]
timeout() int

Returns the network timeout length, in milliseconds.

See also

setTimeout()

New in version 3.6.

Return type:

int

timerEvent(self, QTimerEvent)
useSystemProxy(self) bool

Returns whether the system proxy should be used.

Return type:

bool