QNetworkSessionclass provides control over the system’s access points and enables session management for cases when multiple clients access the same access point. 更多 …
New in version 4.7.
def
activeTime
()
def
bytesReceived
()
def
bytesWritten
()
def
configuration
()
def
error
()
def
errorString
()
def
interface
()
def
isOpen
()
def
sessionProperty
(key)
def
setSessionProperty
(key, value)
def
state
()
def
usagePolicies
()
def
waitForOpened
([msecs=30000])
def
closed
()
def
error
(arg__1)
def
newConfigurationActivated
()
def
opened
()
def
preferredConfigurationChanged
(config, isSeamless)
def
stateChanged
(arg__1)
def
usagePoliciesChanged
(usagePolicies)
A
QNetworkSessionenables control over the system’s network interfaces. The session’s configuration parameter are determined via theQNetworkConfigurationobject to which it is bound. Depending on the type of the session (single access point or service network) a session may be linked to one or more network interfaces. By means ofopeningandclosingof network sessions a developer can start and stop the systems network interfaces. If the configuration represents multiple access points (seeServiceNetwork) more advanced features such as roaming may be supported.
QNetworkSessionsupports session management within the same process and depending on the platform’s capabilities may support out-of-process sessions. If the same network configuration is used by multiple open sessions the underlying network interface is only terminated once the last session has been closed.
Applications may connect to the
preferredConfigurationChanged()signal in order to receive notifications when a more suitable access point becomes available. In response to this signal the application must either initiate the roaming viamigrate()orignore()the new access point. Once the session has roamed thenewConfigurationActivated()signal is emitted. The application may now test the carrier and must eitheraccept()orreject()it. The session will return to the previous access point if the roaming was rejected. The subsequent state diagram depicts the required state transitions.![]()
Some platforms may distinguish forced roaming and application level roaming (ALR). ALR implies that the application controls (via
migrate(),ignore(),accept()andreject()) whether a network session can roam from one access point to the next. Such control is useful if the application maintains stateful socket connections and wants to control the transition from one interface to the next. Forced roaming implies that the system automatically roams to the next network without consulting the application. This has the advantage that the application can make use of roaming features without actually being aware of it. It is expected that the application detects that the underlying socket is broken and automatically reconnects via the new network link.If the platform supports both modes of roaming, an application indicates its preference by connecting to the
preferredConfigurationChanged()signal. Connecting to this signal means that the application wants to take control over the roaming behavior and therefore implies application level roaming. If the client does not connect to thepreferredConfigurationChanged(), forced roaming is used. If forced roaming is not supported the network session will not roam by default.Some applications may want to suppress any form of roaming altogether. Possible use cases may be high priority downloads or remote services which cannot handle a roaming enabled client. Clients can suppress roaming by connecting to the
preferredConfigurationChanged()signal and answer each signal emission withignore().
QNetworkSession
(
connConfig
[
,
parent=None
]
)
¶
- param connConfig
- param parent
QObject
Constructs a session based on
connectionConfig
采用给定
parent
.
PySide2.QtNetwork.QNetworkSession.
State
¶
This enum describes the connectivity state of the session. If the session is based on a single access point configuration the state of the session is the same as the state of the associated network interface.
|
常量 |
描述 |
|---|---|
|
QNetworkSession.Invalid |
The session is invalid due to an invalid configuration. This may happen due to a removed access point or a configuration that was invalid to begin with. |
|
QNetworkSession.NotAvailable |
The session is based on a defined but not yet discovered
|
|
QNetworkSession.Connecting |
The network session is being established. |
|
QNetworkSession.Connected |
The network session is connected. If the current process wishes to use this session it has to register its interest by calling
|
|
QNetworkSession.Closing |
The network session is in the process of being shut down. |
|
QNetworkSession.Disconnected |
The network session is not connected. The associated
|
|
QNetworkSession.Roaming |
The network session is roaming from one access point to another access point. |
New in version 4.7.
PySide2.QtNetwork.QNetworkSession.
SessionError
¶
This enum describes the session errors that can occur.
|
常量 |
描述 |
|---|---|
|
QNetworkSession.UnknownSessionError |
发生无法识别的错误。 |
|
QNetworkSession.SessionAbortedError |
The session was aborted by the user or system. |
|
QNetworkSession.RoamingError |
The session cannot roam to a new configuration. |
|
QNetworkSession.OperationNotSupportedError |
The operation is not supported for current configuration. |
|
QNetworkSession.InvalidConfigurationError |
The operation cannot currently be performed for the current configuration. |
New in version 4.7.
PySide2.QtNetwork.QNetworkSession.
UsagePolicy
¶
These flags allow the system to inform the application of network usage restrictions that may be in place.
|
常量 |
描述 |
|---|---|
|
QNetworkSession.NoPolicy |
No policy in force, usage is unrestricted. |
|
QNetworkSession.NoBackgroundTrafficPolicy |
Background network traffic (not user initiated) should be avoided for example to save battery or data charges |
PySide2.QtNetwork.QNetworkSession.
accept
(
)
¶
Instructs the session to permanently accept the new access point. Once this function has been called the session may not return to the old access point.
The old access point may be closed in the process if there are no other network sessions for it. Therefore any open socket that still uses the old access point may become unusable and should be closed before completing the migration.
PySide2.QtNetwork.QNetworkSession.
activeTime
(
)
¶
quint64
Returns the number of seconds that the session has been active.
PySide2.QtNetwork.QNetworkSession.
bytesReceived
(
)
¶
quint64
Returns the amount of data received in bytes; otherwise 0.
This field value includes the usage across all open network sessions which use the same network interface.
If the session is based on a service network configuration the number of sent bytes across all active member configurations are returned.
This function may not always be supported on all platforms and returns 0. The platform capability can be detected via
DataStatistics
.
注意
On some platforms this function may run the main event loop.
PySide2.QtNetwork.QNetworkSession.
bytesWritten
(
)
¶
quint64
Returns the amount of data sent in bytes; otherwise 0.
This field value includes the usage across all open network sessions which use the same network interface.
If the session is based on a service network configuration the number of sent bytes across all active member configurations are returned.
This function may not always be supported on all platforms and returns 0. The platform capability can be detected via
DataStatistics
.
注意
On some platforms this function may run the main event loop.
PySide2.QtNetwork.QNetworkSession.
close
(
)
¶
Decreases the session counter on the associated network configuration. If the session counter reaches zero the active network interface is shut down. This also means that
state()
will only change from
Connected
to
Disconnected
if the current session was the last open session.
If the platform does not support out-of-process sessions calling this function does not stop the interface. In this case
stop()
has to be used to force a shut down. The platform capabilities can be detected via
capabilities()
.
Note that this call is asynchronous. Depending on the outcome of this call the results can be enquired by connecting to the
stateChanged()
,
opened()
or
error()
signals.
PySide2.QtNetwork.QNetworkSession.
closed
(
)
¶
PySide2.QtNetwork.QNetworkSession.
configuration
(
)
¶
返回
QNetworkConfiguration
that this network session object is based on.
PySide2.QtNetwork.QNetworkSession.
error
(
)
¶
返回最后发生的错误类型。
另请参阅
PySide2.QtNetwork.QNetworkSession.
error
(
arg__1
)
¶
arg__1
–
SessionError
PySide2.QtNetwork.QNetworkSession.
ignore
(
)
¶
This function indicates that the application does not wish to roam the session.
另请参阅
PySide2.QtNetwork.QNetworkSession.
interface
(
)
¶
Returns the network interface that is used by this session.
This function only returns a valid
QNetworkInterface
when this session is
Connected
.
The returned interface may change as a result of a roaming process.
另请参阅
PySide2.QtNetwork.QNetworkSession.
isOpen
(
)
¶
bool
返回
true
if this session is open. If the number of all open sessions is greater than zero the underlying network interface will remain connected/up.
PySide2.QtNetwork.QNetworkSession.
migrate
(
)
¶
Instructs the session to roam to the new access point. The old access point remains active until the application calls
accept()
.
newConfigurationActivated()
signal is emitted once roaming has been completed.
另请参阅
PySide2.QtNetwork.QNetworkSession.
newConfigurationActivated
(
)
¶
PySide2.QtNetwork.QNetworkSession.
open
(
)
¶
Creates an open session which increases the session counter on the underlying network interface. The system will not terminate a network interface until the session reference counter reaches zero. Therefore an open session allows an application to register its use of the interface.
As a result of calling the interface will be started if it is not connected/up yet. Some platforms may not provide support for out-of-process sessions. On such platforms the session counter ignores any sessions held by another process. The platform capabilities can be detected via
capabilities()
.
Note that this call is asynchronous. Depending on the outcome of this call the results can be enquired by connecting to the
stateChanged()
,
opened()
or
error()
signals.
It is not a requirement to open a session in order to monitor the underlying network interface.
PySide2.QtNetwork.QNetworkSession.
opened
(
)
¶
PySide2.QtNetwork.QNetworkSession.
preferredConfigurationChanged
(
config
,
isSeamless
)
¶
config
–
QNetworkConfiguration
isSeamless
–
bool
PySide2.QtNetwork.QNetworkSession.
reject
(
)
¶
The new access point is not suitable for the application. By calling this function the session returns to the previous access point/configuration. This action may invalidate any socket that has been created via the not desired access point.
另请参阅
PySide2.QtNetwork.QNetworkSession.
sessionProperty
(
key
)
¶
key – unicode
object
Returns the value for property
key
.
A network session can have properties attached which may describe the session in more details. This function can be used to gain access to those properties.
The following property keys are guaranteed to be specified on all platforms:
|
Key |
描述 |
|
ActiveConfiguration |
If the session
|
|
UserChoiceConfiguration |
If the session
|
|
ConnectInBackground |
把此特性设为
true
before calling
|
|
AutoCloseSessionTimeout |
If the session requires polling to keep its state up to date, this property holds the timeout in milliseconds before the session will automatically close. If the value of this property is -1 the session will not automatically close. This property is set to -1 by default.
|
另请参阅
PySide2.QtNetwork.QNetworkSession.
setSessionProperty
(
key
,
value
)
¶
key – unicode
value – object
设置特性
value
on the session. The property is identified using
key
. Removing an already set property can be achieved by passing an invalid
QVariant
.
注意: UserChoiceConfiguration and ActiveConfiguration properties are read only and cannot be changed using this method.
另请参阅
PySide2.QtNetwork.QNetworkSession.
state
(
)
¶
Returns the state of the session.
If the session is based on a single access point configuration the state of the session is the same as the state of the associated network interface. Therefore a network session object can be used to monitor network interfaces.
A
ServiceNetwork
based session summarizes the state of all its children and therefore returns the
Connected
state if at least one of the service network’s
children()
configurations is active.
Note that it is not required to hold an open session in order to obtain the network interface state. A connected but closed session may be used to monitor network interfaces whereas an open and connected session object may prevent the network interface from being shut down.
另请参阅
PySide2.QtNetwork.QNetworkSession.
stop
(
)
¶
Invalidates all open sessions against the network interface and therefore stops the underlying network interface. This function always changes the session’s
state()
flag to
Disconnected
.
PySide2.QtNetwork.QNetworkSession.
usagePolicies
(
)
¶
UsagePolicies
Returns the network usage policies currently in force by the system.
PySide2.QtNetwork.QNetworkSession.
usagePoliciesChanged
(
usagePolicies
)
¶
usagePolicies
–
UsagePolicies
PySide2.QtNetwork.QNetworkSession.
waitForOpened
(
[
msecs=30000
]
)
¶
msecs
–
int
bool
Waits until the session has been opened, up to
msecs
milliseconds. If the session has been opened, this function returns
true
;否则它返回
false
。若它返回
false
,可以调用
error()
to determine the cause of the error.
The following example waits up to one second for the session to be opened:
session->open();
if (session->waitForOpened(1000))
qDebug("Open!");
若
msecs
是 -1,此函数不会超时。