QNetworkAccessManager

Synopsis

Functions

Virtual functions

Signals

Detailed Description

The PySide.QtNetwork.QNetworkAccessManager class allows the application to send network requests and receive replies

The Network Access API is constructed around one PySide.QtNetwork.QNetworkAccessManager object, which holds the common configuration and settings for the requests it sends. It contains the proxy and cache configuration, as well as the signals related to such issues, and reply signals that can be used to monitor the progress of a network operation. One PySide.QtNetwork.QNetworkAccessManager should be enough for the whole Qt application.

Once a PySide.QtNetwork.QNetworkAccessManager object has been created, the application can use it to send requests over the network. A group of standard functions are supplied that take a request and optional data, and each return a PySide.QtNetwork.QNetworkReply object. The returned object is used to obtain any data returned in response to the corresponding request.

A simple download off the network could be accomplished with:

manager = QNetworkAccessManager(self)
manager.finished[QNetworkReply].connect(self.replyFinished)

manager.get(QNetworkRequest(QUrl("http://qt.nokia.com")))

PySide.QtNetwork.QNetworkAccessManager has an asynchronous API. When the replyFinished slot above is called, the parameter it takes is the PySide.QtNetwork.QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.).

Note

After the request has finished, it is the responsibility of the user to delete the PySide.QtNetwork.QNetworkReply object at an appropriate time. Do not directly delete it inside the slot connected to PySide.QtNetwork.QNetworkAccessManager.finished() . You can use the PySide.QtCore.QObject.deleteLater() function.

Note

PySide.QtNetwork.QNetworkAccessManager queues the requests it receives. The number of requests executed in parallel is dependent on the protocol. Currently, for the HTTP protocol on desktop platforms, 6 requests are executed in parallel for one host/port combination.

A more involved example, assuming the manager is already existent, can be:

request = QNetworkRequest()
request.setUrl(QUrl("http://qt.nokia.com"))
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0")

reply = manager.get(request)
reply.readyRead.connect(self.slotReadyRead)
reply.error[QNetworkReply.NetworkError].connect(self..slotError)
reply.sslErrors.connect(self.slotSslErrors)

Network and Roaming support

With the addition of the Bearer Management API to Qt 4.7 PySide.QtNetwork.QNetworkAccessManager gained the ability to manage network connections. PySide.QtNetwork.QNetworkAccessManager can start the network interface if the device is offline and terminates the interface if the current process is the last one to use the uplink. Note that some platform utilize grace periods from when the last application stops using a uplink until the system actually terminates the connectivity link. Roaming is equally transparent. Any queued/pending network requests are automatically transferred to new access point.

Clients wanting to utilize this feature should not require any changes. In fact it is likely that existing platform specific connection code can simply be removed from the application.

Note

The network and roaming support in PySide.QtNetwork.QNetworkAccessManager is conditional upon the platform supporting connection management. The QNetworkConfigurationManager.NetworkSessionRequired can be used to detect whether PySide.QtNetwork.QNetworkAccessManager utilizes this feature. Currently only Meego/Harmattan and Symbian platforms provide connection management support.

Note

This feature cannot be used in combination with the Bearer Management API as provided by QtMobility. Applications have to migrate to the Qt version of Bearer Management.

Symbian Platform Security Requirements

On Symbian, processes which use this class must have the NetworkServices platform security capability. If the client process lacks this capability, operations will result in a panic.

Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.

class PySide.QtNetwork.QNetworkAccessManager([parent=None])
Parameters:parentPySide.QtCore.QObject

Constructs a PySide.QtNetwork.QNetworkAccessManager object that is the center of the Network Access API and sets parent as the parent object.

PySide.QtNetwork.QNetworkAccessManager.NetworkAccessibility

Indicates whether the network is accessible via this network access manager.

Constant Description
QNetworkAccessManager.UnknownAccessibility The network accessibility cannot be determined.
QNetworkAccessManager.NotAccessible The network is not currently accessible, either because there is currently no network coverage or network access has been explicitly disabled by a call to PySide.QtNetwork.QNetworkAccessManager.setNetworkAccessible() .
QNetworkAccessManager.Accessible The network is accessible.

Note

This enum was introduced or modified in Qt 4.7

PySide.QtNetwork.QNetworkAccessManager.Operation

Indicates the operation this reply is processing.

Constant Description
QNetworkAccessManager.HeadOperation retrieve headers operation (created with PySide.QtNetwork.QNetworkAccessManager.head() )
QNetworkAccessManager.GetOperation retrieve headers and download contents (created with PySide.QtNetwork.QNetworkAccessManager.get() )
QNetworkAccessManager.PutOperation upload contents operation (created with PySide.QtNetwork.QNetworkAccessManager.put() )
QNetworkAccessManager.PostOperation send the contents of an HTML form for processing via HTTP POST (created with PySide.QtNetwork.QNetworkAccessManager.post() )
QNetworkAccessManager.DeleteOperation delete contents operation (created with PySide.QtNetwork.QNetworkAccessManager.deleteResource() )
QNetworkAccessManager.CustomOperation custom operation (created with PySide.QtNetwork.QNetworkAccessManager.sendCustomRequest() )
PySide.QtNetwork.QNetworkAccessManager.activeConfiguration()
Return type:PySide.QtNetwork.QNetworkConfiguration

Returns the current active network configuration.

If the network configuration returned by PySide.QtNetwork.QNetworkAccessManager.configuration() is of type QNetworkConfiguration.ServiceNetwork this function will return the current active child network configuration of that configuration. Otherwise returns the same network configuration as PySide.QtNetwork.QNetworkAccessManager.configuration() .

Use this function to return the actual network configuration currently in use by the network session.

PySide.QtNetwork.QNetworkAccessManager.authenticationRequired(reply, authenticator)
Parameters:
PySide.QtNetwork.QNetworkAccessManager.cache()
Return type:PySide.QtNetwork.QAbstractNetworkCache

Returns the cache that is used to store data obtained from the network.

PySide.QtNetwork.QNetworkAccessManager.configuration()
Return type:PySide.QtNetwork.QNetworkConfiguration

Returns the network configuration that will be used to create the network session which will be used when processing network requests.

PySide.QtNetwork.QNetworkAccessManager.cookieJar()
Return type:PySide.QtNetwork.QNetworkCookieJar

Returns the PySide.QtNetwork.QNetworkCookieJar that is used to store cookies obtained from the network as well as cookies that are about to be sent.

PySide.QtNetwork.QNetworkAccessManager.createRequest(op, request[, outgoingData=None])
Parameters:
Return type:

PySide.QtNetwork.QNetworkReply

Returns a new PySide.QtNetwork.QNetworkReply object to handle the operation op and request req . The device outgoingData is always 0 for Get and Head requests, but is the value passed to PySide.QtNetwork.QNetworkAccessManager.post() and PySide.QtNetwork.QNetworkAccessManager.put() in those operations (the PySide.QtCore.QByteArray variants will pass a PySide.QtCore.QBuffer object).

The default implementation calls QNetworkCookieJar.cookiesForUrl() on the cookie jar set with PySide.QtNetwork.QNetworkAccessManager.setCookieJar() to obtain the cookies to be sent to the remote server.

The returned object must be in an open state.

PySide.QtNetwork.QNetworkAccessManager.deleteResource(request)
Parameters:requestPySide.QtNetwork.QNetworkRequest
Return type:PySide.QtNetwork.QNetworkReply

Sends a request to delete the resource identified by the URL of request .

Note

This feature is currently available for HTTP only, performing an HTTP DELETE request.

PySide.QtNetwork.QNetworkAccessManager.finished(reply)
Parameters:replyPySide.QtNetwork.QNetworkReply
PySide.QtNetwork.QNetworkAccessManager.get(request)
Parameters:requestPySide.QtNetwork.QNetworkRequest
Return type:PySide.QtNetwork.QNetworkReply

Posts a request to obtain the contents of the target request and returns a new PySide.QtNetwork.QNetworkReply object opened for reading which emits the PySide.QtCore.QIODevice.readyRead() signal whenever new data arrives.

The contents as well as associated headers will be downloaded.

PySide.QtNetwork.QNetworkAccessManager.head(request)
Parameters:requestPySide.QtNetwork.QNetworkRequest
Return type:PySide.QtNetwork.QNetworkReply

Posts a request to obtain the network headers for request and returns a new PySide.QtNetwork.QNetworkReply object which will contain such headers.

The function is named after the HTTP request associated (HEAD).

PySide.QtNetwork.QNetworkAccessManager.networkAccessible()
Return type:PySide.QtNetwork.QNetworkAccessManager.NetworkAccessibility

This property holds whether the network is currently accessible via this network access manager..

If the network is not accessible the network access manager will not process any new network requests, all such requests will fail with an error. Requests with URLs with the file:// scheme will still be processed.

By default the value of this property reflects the physical state of the device. Applications may override it to disable all network requests via this network access manager by calling

networkAccessManager.setNetworkAccessible(QNetworkAccessManager.NotAccessible)

Network requests can be reenabled again by calling

networkAccessManager.setNetworkAccessible(QNetworkAccessManager.Accessible)

Note

Calling PySide.QtNetwork.QNetworkAccessManager.setNetworkAccessible() does not change the network state.

PySide.QtNetwork.QNetworkAccessManager.networkAccessibleChanged(accessible)
Parameters:accessiblePySide.QtNetwork.QNetworkAccessManager.NetworkAccessibility
PySide.QtNetwork.QNetworkAccessManager.networkSessionConnected()
PySide.QtNetwork.QNetworkAccessManager.post(request, data)
Parameters:
Return type:

PySide.QtNetwork.QNetworkReply

This is an overloaded function.

Sends the contents of the data byte array to the destination specified by request .

PySide.QtNetwork.QNetworkAccessManager.post(request, data)
Parameters:
Return type:

PySide.QtNetwork.QNetworkReply

Sends an HTTP POST request to the destination specified by request and returns a new PySide.QtNetwork.QNetworkReply object opened for reading that will contain the reply sent by the server. The contents of the data device will be uploaded to the server.

data must be open for reading and must remain valid until the PySide.QtNetwork.QNetworkAccessManager.finished() signal is emitted for this reply.

Note

Sending a POST request on protocols other than HTTP and HTTPS is undefined and will probably fail.

PySide.QtNetwork.QNetworkAccessManager.proxy()
Return type:PySide.QtNetwork.QNetworkProxy

Returns the PySide.QtNetwork.QNetworkProxy that the requests sent using this PySide.QtNetwork.QNetworkAccessManager object will use. The default value for the proxy is QNetworkProxy.DefaultProxy .

PySide.QtNetwork.QNetworkAccessManager.proxyAuthenticationRequired(proxy, authenticator)
Parameters:
PySide.QtNetwork.QNetworkAccessManager.proxyFactory()
Return type:PySide.QtNetwork.QNetworkProxyFactory

Returns the proxy factory that this PySide.QtNetwork.QNetworkAccessManager object is using to determine the proxies to be used for requests.

Note that the pointer returned by this function is managed by PySide.QtNetwork.QNetworkAccessManager and could be deleted at any time.

PySide.QtNetwork.QNetworkAccessManager.put(request, data)
Parameters:
Return type:

PySide.QtNetwork.QNetworkReply

Uploads the contents of data to the destination request and returnes a new PySide.QtNetwork.QNetworkReply object that will be open for reply.

data must be opened for reading when this function is called and must remain valid until the PySide.QtNetwork.QNetworkAccessManager.finished() signal is emitted for this reply.

Whether anything will be available for reading from the returned object is protocol dependent. For HTTP, the server may send a small HTML page indicating the upload was successful (or not). Other protocols will probably have content in their replies.

Note

For HTTP, this request will send a PUT request, which most servers do not allow. Form upload mechanisms, including that of uploading files through HTML forms, use the POST mechanism.

PySide.QtNetwork.QNetworkAccessManager.put(request, data)
Parameters:
Return type:

PySide.QtNetwork.QNetworkReply

This is an overloaded function.

Sends the contents of the data byte array to the destination specified by request .

PySide.QtNetwork.QNetworkAccessManager.sendCustomRequest(request, verb[, data=None])
Parameters:
Return type:

PySide.QtNetwork.QNetworkReply

Sends a custom request to the server identified by the URL of request .

It is the user’s responsibility to send a verb to the server that is valid according to the HTTP specification.

This method provides means to send verbs other than the common ones provided via PySide.QtNetwork.QNetworkAccessManager.get() or PySide.QtNetwork.QNetworkAccessManager.post() etc., for instance sending an HTTP OPTIONS command.

If data is not empty, the contents of the data device will be uploaded to the server; in that case, data must be open for reading and must remain valid until the PySide.QtNetwork.QNetworkAccessManager.finished() signal is emitted for this reply.

Note

This feature is currently available for HTTP(S) only.

PySide.QtNetwork.QNetworkAccessManager.setCache(cache)
Parameters:cachePySide.QtNetwork.QAbstractNetworkCache

Sets the manager’s network cache to be the cache specified. The cache is used for all requests dispatched by the manager.

Use this function to set the network cache object to a class that implements additional features, like saving the cookies to permanent storage.

Note

PySide.QtNetwork.QNetworkAccessManager takes ownership of the cache object.

PySide.QtNetwork.QNetworkAccessManager by default does not have a set cache. Qt provides a simple disk cache, PySide.QtNetwork.QNetworkDiskCache , which can be used.

See also

PySide.QtNetwork.QNetworkAccessManager.cache() QNetworkRequest.CacheLoadControl

PySide.QtNetwork.QNetworkAccessManager.setConfiguration(config)
Parameters:configPySide.QtNetwork.QNetworkConfiguration

Sets the network configuration that will be used when creating the network session to config .

The network configuration is used to create and open a network session before any request that requires network access is process. If no network configuration is explicitly set via this function the network configuration returned by QNetworkConfigurationManager.defaultConfiguration() will be used.

To restore the default network configuration set the network configuration to the value returned from QNetworkConfigurationManager.defaultConfiguration() .

manager = QNetworkConfigurationManager()
networkAccessManager.setConfiguration(manager.defaultConfiguration())

If an invalid network configuration is set, a network session will not be created. In this case network requests will be processed regardless, but may fail. For example:

networkAccessManager.setConfiguration(QNetworkConfiguration())
PySide.QtNetwork.QNetworkAccessManager.setCookieJar(cookieJar)
Parameters:cookieJarPySide.QtNetwork.QNetworkCookieJar

Sets the manager’s cookie jar to be the cookieJar specified. The cookie jar is used by all requests dispatched by the manager.

Use this function to set the cookie jar object to a class that implements additional features, like saving the cookies to permanent storage.

Note

PySide.QtNetwork.QNetworkAccessManager takes ownership of the cookieJar object.

If cookieJar is in the same thread as this PySide.QtNetwork.QNetworkAccessManager , it will set the parent of the cookieJar so that the cookie jar is deleted when this object is deleted as well. If you want to share cookie jars between different PySide.QtNetwork.QNetworkAccessManager objects, you may want to set the cookie jar’s parent to 0 after calling this function.

PySide.QtNetwork.QNetworkAccessManager by default does not implement any cookie policy of its own: it accepts all cookies sent by the server, as long as they are well formed and meet the minimum security requirements (cookie domain matches the request’s and cookie path matches the request’s). In order to implement your own security policy, override the QNetworkCookieJar.cookiesForUrl() and QNetworkCookieJar.setCookiesFromUrl() virtual functions. Those functions are called by PySide.QtNetwork.QNetworkAccessManager when it detects a new cookie.

PySide.QtNetwork.QNetworkAccessManager.setNetworkAccessible(accessible)
Parameters:accessiblePySide.QtNetwork.QNetworkAccessManager.NetworkAccessibility

This property holds whether the network is currently accessible via this network access manager..

If the network is not accessible the network access manager will not process any new network requests, all such requests will fail with an error. Requests with URLs with the file:// scheme will still be processed.

By default the value of this property reflects the physical state of the device. Applications may override it to disable all network requests via this network access manager by calling

networkAccessManager.setNetworkAccessible(QNetworkAccessManager.NotAccessible)

Network requests can be reenabled again by calling

networkAccessManager.setNetworkAccessible(QNetworkAccessManager.Accessible)

Note

Calling PySide.QtNetwork.QNetworkAccessManager.setNetworkAccessible() does not change the network state.

PySide.QtNetwork.QNetworkAccessManager.setProxy(proxy)
Parameters:proxyPySide.QtNetwork.QNetworkProxy

Sets the proxy to be used in future requests to be proxy . This does not affect requests that have already been sent. The PySide.QtNetwork.QNetworkAccessManager.proxyAuthenticationRequired() signal will be emitted if the proxy requests authentication.

A proxy set with this function will be used for all requests issued by PySide.QtNetwork.QNetworkAccessManager . In some cases, it might be necessary to select different proxies depending on the type of request being sent or the destination host. If that’s the case, you should consider using PySide.QtNetwork.QNetworkAccessManager.setProxyFactory() .

PySide.QtNetwork.QNetworkAccessManager.setProxyFactory(factory)
Parameters:factoryPySide.QtNetwork.QNetworkProxyFactory

Sets the proxy factory for this class to be factory . A proxy factory is used to determine a more specific list of proxies to be used for a given request, instead of trying to use the same proxy value for all requests.

All queries sent by PySide.QtNetwork.QNetworkAccessManager will have type QNetworkProxyQuery.UrlRequest .

For example, a proxy factory could apply the following rules:

  • if the target address is in the local network (for example, if the hostname contains no dots or if it’s an IP address in the organization’s range), return QNetworkProxy.NoProxy
  • if the request is FTP, return an FTP proxy
  • if the request is HTTP or HTTPS, then return an HTTP proxy
  • otherwise, return a SOCKSv5 proxy server

The lifetime of the object factory will be managed by PySide.QtNetwork.QNetworkAccessManager . It will delete the object when necessary.

Note

If a specific proxy is set with PySide.QtNetwork.QNetworkAccessManager.setProxy() , the factory will not be used.

PySide.QtNetwork.QNetworkAccessManager.sslErrors(reply, errors)
Parameters: