QNetworkAccessManagerclass allows the application to send network requests and receive replies. 更多 …
def
activeConfiguration
()
def
addStrictTransportSecurityHosts
(knownHosts)
def
autoDeleteReplies
()
def
cache
()
def
clearAccessCache
()
def
clearConnectionCache
()
def
configuration
()
def
connectToHost
(hostName[, port=80])
def
connectToHostEncrypted
(hostName, port, sslConfiguration, peerName)
def
connectToHostEncrypted
(hostName[, port=443[, sslConfiguration=QSslConfiguration.defaultConfiguration()]])
def
cookieJar
()
def
deleteResource
(request)
def
enableStrictTransportSecurityStore
(enabled[, storeDir=””])
def
get
(request)
def
head
(request)
def
networkAccessible
()
def
post
(request, data)
def
post
(request, data)
def
post
(request, multiPart)
def
proxy
()
def
proxyFactory
()
def
put
(request, data)
def
put
(request, data)
def
put
(request, multiPart)
def
redirectPolicy
()
def
sendCustomRequest
(request, verb, data)
def
sendCustomRequest
(request, verb, multiPart)
def
sendCustomRequest
(request, verb[, data=None])
def
setAutoDeleteReplies
(autoDelete)
def
setCache
(cache)
def
setConfiguration
(config)
def
setCookieJar
(cookieJar)
def
setNetworkAccessible
(accessible)
def
setProxy
(proxy)
def
setProxyFactory
(factory)
def
setRedirectPolicy
(policy)
def
setStrictTransportSecurityEnabled
(enabled)
def
setTransferTimeout
([timeout=QNetworkRequest.DefaultTransferTimeoutConstant])
def
strictTransportSecurityHosts
()
def
supportedSchemes
()
def
transferTimeout
()
def
createRequest
(op, request[, outgoingData=None])
def
authenticationRequired
(reply, authenticator)
def
encrypted
(reply)
def
finished
(reply)
def
networkAccessibleChanged
(accessible)
def
networkSessionConnected
()
def
preSharedKeyAuthenticationRequired
(reply, authenticator)
def
proxyAuthenticationRequired
(proxy, authenticator)
def
sslErrors
(reply, errors)
The Network Access API is constructed around one
QNetworkAccessManagerobject, 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. OneQNetworkAccessManagerinstance should be enough for the whole Qt application. SinceQNetworkAccessManager基于QObject,它只能在其所属的线程中使用。一旦
QNetworkAccessManagerobject 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 aQNetworkReply对象。返回对象被用于获取响应相应请求,而返回的任何数据。简单下载断网,可以被贯彻采用:
manager = QNetworkAccessManager(self) manager.finished[QNetworkReply].connect(self.replyFinished) manager.get(QNetworkRequest(QUrl("http://qt-project.org")))
QNetworkAccessManagerhas an asynchronous API. When thereplyFinished槽被调用时,它接受的参数是QNetworkReply对象,包含下载数据及元数据 (Header 头、等)。注意
在请求完成后,用户有责任删除
QNetworkReply对象,在适当的时候。不要直接在槽内把它删除,槽被连接到finished(). You can use thedeleteLater()函数。注意
QNetworkAccessManagerqueues 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.假定管理器已经存在,更复杂范例可以是:
request = QNetworkRequest() request.setUrl(QUrl("http://qt-project.org")) 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)
QNetworkAccessManager
(
[
parent=None
]
)
¶
- param parent
QObject
构造
QNetworkAccessManager
object that is the center of the Network Access API and sets
parent
作为父级对象。
PySide2.QtNetwork.QNetworkAccessManager.
操作
¶
指示此回复正在处理的操作。
|
常量 |
描述 |
|---|---|
|
QNetworkAccessManager.HeadOperation |
检索 Header 头操作 (创建采用
|
|
QNetworkAccessManager.GetOperation |
检索 Header 头并下载内容 (创建采用
|
|
QNetworkAccessManager.PutOperation |
上传内容操作 (创建采用
|
|
QNetworkAccessManager.PostOperation |
经由 HTTP POST 发送用于处理的 HTML 表单内容 (创建采用
|
|
QNetworkAccessManager.DeleteOperation |
删除内容操作 (创建采用
|
|
QNetworkAccessManager.CustomOperation |
自定义操作 (创建采用
|
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
NetworkAccessibility
¶
Indicates whether the network is accessible via this network access manager.
|
常量 |
描述 |
|---|---|
|
QNetworkAccessManager.UnknownAccessibility |
网络的可访问性不能确定。 |
|
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
|
|
QNetworkAccessManager.Accessible |
网络是可访问的。 |
另请参阅
New in version 4.7.
PySide2.QtNetwork.QNetworkAccessManager.
activeConfiguration
(
)
¶
注意
此函数被弃用。
Returns the current active network configuration.
If the network configuration returned by
configuration()
是类型
ServiceNetwork
this function will return the current active child network configuration of that configuration. Otherwise returns the same network configuration as
configuration()
.
Use this function to return the actual network configuration currently in use by the network session.
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
addStrictTransportSecurityHosts
(
knownHosts
)
¶
knownHosts –
将 HSTS (HTTP 严格传输安全) 策略添加到 HSTS 缓存。
knownHosts
contains the known hosts that have
QHstsPolicy
information.
注意
An expired policy will remove a known host from the cache, if previously present.
注意
当处理 HTTP 响应,
QNetworkAccessManager
can also update the HSTS cache, removing or updating exitsting policies or introducing new
knownHosts
. The current implementation thus is server-driven, client code can provide
QNetworkAccessManager
with previously known or discovered policies, but this information can be overridden by “Strict-Transport-Security” response headers.
PySide2.QtNetwork.QNetworkAccessManager.
authenticationRequired
(
reply
,
authenticator
)
¶
reply
–
QNetworkReply
authenticator
–
QAuthenticator
PySide2.QtNetwork.QNetworkAccessManager.
autoDeleteReplies
(
)
¶
bool
返回 true 若
QNetworkAccessManager
目前被配置为自动删除 QNetworkReplies,否则 false。
另请参阅
setAutoDeleteReplies
AutoDeleteReplyOnFinishAttribute
PySide2.QtNetwork.QNetworkAccessManager.
cache
(
)
¶
返回用于存储从网络获得数据的缓存。
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
clearAccessCache
(
)
¶
刷新身份验证数据和网络连接的内部缓存。
此函数对履行自动测试很有用。
PySide2.QtNetwork.QNetworkAccessManager.
clearConnectionCache
(
)
¶
刷新网络连接的内部缓存。相比
clearAccessCache()
the authentication data is preserved.
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
configuration
(
)
¶
注意
此函数被弃用。
Returns the network configuration that will be used to create the
network
session
which will be used when processing network requests.
PySide2.QtNetwork.QNetworkAccessManager.
connectToHost
(
hostName
[
,
port=80
]
)
¶
hostName – unicode
port
–
quint16
初启连接到主机,给定通过
hostName
在端口
port
。此函数对在发出 HTTP 请求之前完成与主机的 TCP 握手很有用,从而降低网络延迟。
注意
此函数没有可能去报告错误。
PySide2.QtNetwork.QNetworkAccessManager.
connectToHostEncrypted
(
hostName
[
,
port=443
[
,
sslConfiguration=QSslConfiguration.defaultConfiguration()
]
]
)
¶
hostName – unicode
port
–
quint16
sslConfiguration
–
QSslConfiguration
初启连接到主机,给定通过
hostName
在端口
port
,使用
sslConfiguration
. This function is useful to complete the TCP and SSL handshake to a host before the HTTPS request is made, resulting in a lower network latency.
注意
Preconnecting a SPDY connection can be done by calling setAllowedNextProtocols() on
sslConfiguration
with
QSslConfiguration::NextProtocolSpdy3_0
contained in the list of allowed protocols. When using SPDY, one single connection per host is enough, i.e. calling this method multiple times per host will not result in faster network transactions.
注意
此函数没有可能去报告错误。
PySide2.QtNetwork.QNetworkAccessManager.
connectToHostEncrypted
(
hostName
,
port
,
sslConfiguration
,
peerName
)
¶
hostName – unicode
port
–
quint16
sslConfiguration
–
QSslConfiguration
peerName – unicode
这是重载函数。
初启连接到主机,给定通过
hostName
在端口
port
,使用
sslConfiguration
with
peerName
set to be the hostName used for certificate validation. This function is useful to complete the TCP and SSL handshake to a host before the HTTPS request is made, resulting in a lower network latency.
注意
Preconnecting a SPDY connection can be done by calling setAllowedNextProtocols() on
sslConfiguration
with
QSslConfiguration::NextProtocolSpdy3_0
contained in the list of allowed protocols. When using SPDY, one single connection per host is enough, i.e. calling this method multiple times per host will not result in faster network transactions.
注意
此函数没有可能去报告错误。
返回
QNetworkCookieJar
用于存储从网络获得的 Cookie 及即将被发送的 Cookie。
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
createRequest
(
op
,
request
[
,
outgoingData=None
]
)
¶
op
–
操作
request
–
QNetworkRequest
outgoingData
–
QIODevice
返回新
QNetworkReply
对象以处理操作
op
和请求
originalReq
。设备
outgoingData
始终为 0 对于 Get 和 Head 请求而言,但是值被传递给
post()
and
put()
in those operations (the
QByteArray
变体会传递
QBuffer
对象)。
默认实现调用
cookiesForUrl()
on the cookie jar set with
setCookieJar()
to obtain the cookies to be sent to the remote server.
返回的对象必须处于打开状态。
PySide2.QtNetwork.QNetworkAccessManager.
deleteResource
(
request
)
¶
request
–
QNetworkRequest
发送请求以删除资源标识通过 URL 的
request
.
注意
此特征目前只可用于 HTTP,履行 HTTP DELETE 请求。
PySide2.QtNetwork.QNetworkAccessManager.
enableStrictTransportSecurityStore
(
enabled
[
,
storeDir=""
]
)
¶
enabled
–
bool
storeDir – unicode
若
enabled
is
true
,内部 HSTS (HTTP 严格传输安全) 缓存将使用持久存储来读取和写入 HSTS 策略。
storeDir
defines where this store will be located. The default location is defined by
CacheLocation
. If there is no writable QStandartPaths::CacheLocation and
storeDir
is an empty string, the store will be located in the program’s working directory.
注意
If HSTS cache already contains HSTS policies by the time persistent store is enabled, these policies will be preserved in the store. In case both cache and store contain the same known hosts, policies from cache are considered to be more up-to-date (and thus will overwrite the previous values in the store). If this behavior is undesired, enable HSTS store before enabling Strict Tranport Security. By default, the persistent store of HSTS policies is disabled.
另请参阅
isStrictTransportSecurityStoreEnabled()
setStrictTransportSecurityEnabled()
standardLocations()
PySide2.QtNetwork.QNetworkAccessManager.
encrypted
(
reply
)
¶
reply
–
QNetworkReply
PySide2.QtNetwork.QNetworkAccessManager.
finished
(
reply
)
¶
reply
–
QNetworkReply
PySide2.QtNetwork.QNetworkAccessManager.
get
(
request
)
¶
request
–
QNetworkRequest
张贴请求以获取内容对于目标
request
并返回新
QNetworkReply
打开对象为读取其发射的
readyRead()
信号,每当新数据到达时。
内容及关联 Header 头会被下载。
PySide2.QtNetwork.QNetworkAccessManager.
head
(
request
)
¶
request
–
QNetworkRequest
张贴请求以获取网络头为
request
并返回新
QNetworkReply
对象 (包含这样的 Header 头)。
函数以 HTTP 请求关联 HEAD 头命名。
PySide2.QtNetwork.QNetworkAccessManager.
isStrictTransportSecurityEnabled
(
)
¶
bool
返回 true,若 HSTS (HTTP 严格传输安全) 被启用。默认情况下,HSTS 是禁用的。
PySide2.QtNetwork.QNetworkAccessManager.
isStrictTransportSecurityStoreEnabled
(
)
¶
bool
返回 true,若 HSTS (HTTP 严格传输安全) 缓存使用永久存储去加载、存储 HSTS 策略。
PySide2.QtNetwork.QNetworkAccessManager.
networkAccessible
(
)
¶
注意
此函数被弃用。
Returns the current network accessibility.
PySide2.QtNetwork.QNetworkAccessManager.
networkAccessibleChanged
(
accessible
)
¶
accessible
–
NetworkAccessibility
注意
此函数被弃用。
PySide2.QtNetwork.QNetworkAccessManager.
networkSessionConnected
(
)
¶
注意
此函数被弃用。
PySide2.QtNetwork.QNetworkAccessManager.
post
(
request
,
multiPart
)
¶
request
–
QNetworkRequest
multiPart
–
QHttpMultiPart
PySide2.QtNetwork.QNetworkAccessManager.
post
(
request
,
data
)
¶
request
–
QNetworkRequest
data
–
QIODevice
PySide2.QtNetwork.QNetworkAccessManager.
post
(
request
,
data
)
¶
request
–
QNetworkRequest
data
–
QByteArray
reply
–
QNetworkReply
authenticator
–
QSslPreSharedKeyAuthenticator
PySide2.QtNetwork.QNetworkAccessManager.
proxy
(
)
¶
返回
QNetworkProxy
请求发送使用此
QNetworkAccessManager
对象将使用。代理的默认值为
DefaultProxy
.
PySide2.QtNetwork.QNetworkAccessManager.
proxyAuthenticationRequired
(
proxy
,
authenticator
)
¶
proxy
–
QNetworkProxy
authenticator
–
QAuthenticator
PySide2.QtNetwork.QNetworkAccessManager.
proxyFactory
(
)
¶
返回代理工厂,此
QNetworkAccessManager
对象被用于确定要被用于请求的代理。
注意:由此函数返回的指针的管理是通过
QNetworkAccessManager
且可以随时被删除。
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
put
(
request
,
multiPart
)
¶
request
–
QNetworkRequest
multiPart
–
QHttpMultiPart
PySide2.QtNetwork.QNetworkAccessManager.
put
(
request
,
data
)
¶
request
–
QNetworkRequest
data
–
QIODevice
PySide2.QtNetwork.QNetworkAccessManager.
put
(
request
,
data
)
¶
request
–
QNetworkRequest
data
–
QByteArray
PySide2.QtNetwork.QNetworkAccessManager.
redirectPolicy
(
)
¶
RedirectPolicy
返回所用的重定向策略,当创建新请求时。
另请参阅
setRedirectPolicy()
RedirectPolicy
PySide2.QtNetwork.QNetworkAccessManager.
sendCustomRequest
(
request
,
verb
,
data
)
¶
request
–
QNetworkRequest
verb
–
QByteArray
data
–
QByteArray
PySide2.QtNetwork.QNetworkAccessManager.
sendCustomRequest
(
request
,
verb
,
multiPart
)
¶
request
–
QNetworkRequest
verb
–
QByteArray
multiPart
–
QHttpMultiPart
PySide2.QtNetwork.QNetworkAccessManager.
sendCustomRequest
(
request
,
verb
[
,
data=None
]
)
¶
request
–
QNetworkRequest
verb
–
QByteArray
data
–
QIODevice
PySide2.QtNetwork.QNetworkAccessManager.
setAutoDeleteReplies
(
autoDelete
)
¶
autoDelete
–
bool
启用 (或禁用) 自动删除
QNetworkReplies
.
设置
shouldAutoDelete
to true is the same as setting the
AutoDeleteReplyOnFinishAttribute
attribute to true on all
future
QNetworkRequests
passed to this instance of
QNetworkAccessManager
unless the attribute was already explicitly set on the
QNetworkRequest
.
另请参阅
autoDeleteReplies
AutoDeleteReplyOnFinishAttribute
PySide2.QtNetwork.QNetworkAccessManager.
setCache
(
cache
)
¶
cache
–
QAbstractNetworkCache
Sets the manager’s network cache to be the
cache
指定。缓存用于由管理器分派的所有请求。
Use this function to set the network cache object to a class that implements additional features, like saving the cookies to permanent storage.
注意
QNetworkAccessManager
拥有所有权对于
cache
对象。
QNetworkAccessManager
by default does not have a set cache. Qt provides a simple disk cache,
QNetworkDiskCache
, which can be used.
另请参阅
cache()
CacheLoadControl
PySide2.QtNetwork.QNetworkAccessManager.
setConfiguration
(
config
)
¶
config
–
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
defaultConfiguration()
会被使用。
To restore the default network configuration set the network configuration to the value returned from
defaultConfiguration()
.
Setting a network configuration means that the
QNetworkAccessManager
instance will only be using the specified one. In particular, if the default network configuration changes (upon e.g. Wifi being available), this new configuration needs to be enabled manually if desired.
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())
PySide2.QtNetwork.QNetworkAccessManager.
setCookieJar
(
cookieJar
)
¶
cookieJar
–
QNetworkCookieJar
Sets the manager’s cookie jar to be the
cookieJar
指定。Cookie Jar 用于由管理器分派的所有请求。
Use this function to set the cookie jar object to a class that implements additional features, like saving the cookies to permanent storage.
注意
QNetworkAccessManager
拥有所有权对于
cookieJar
对象。
若
cookieJar
is in the same thread as this
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
QNetworkAccessManager
objects, you may want to set the cookie jar’s parent to 0 after calling this function.
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
cookiesForUrl()
and
setCookiesFromUrl()
virtual functions. Those functions are called by
QNetworkAccessManager
when it detects a new cookie.
PySide2.QtNetwork.QNetworkAccessManager.
setNetworkAccessible
(
accessible
)
¶
accessible
–
NetworkAccessibility
注意
此函数被弃用。
Overrides the reported network accessibility. If
accessible
is
NotAccessible
the reported network accessiblity will always be
NotAccessible
. Otherwise the reported network accessibility will reflect the actual device state.
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
setProxy
(
proxy
)
¶
proxy
–
QNetworkProxy
将用于未来请求的代理设为
proxy
。这不会影响已经发送的请求。
proxyAuthenticationRequired()
signal will be emitted if the proxy requests authentication.
A proxy set with this function will be used for all requests issued by
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
setProxyFactory()
.
PySide2.QtNetwork.QNetworkAccessManager.
setProxyFactory
(
factory
)
¶
factory
–
QNetworkProxyFactory
将此类的代理工厂设为
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
QNetworkAccessManager
will have type
UrlRequest
.
例如,代理工厂可以应用以下规则:
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
NoProxy
若请求为 FTP,返回 FTP 代理
若请求为 HTTP 或 HTTPS,返回 HTTP 代理
否则,返回 SOCKSv5 代理服务器
The lifetime of the object
factory
will be managed by
QNetworkAccessManager
。它会删除对象当有必要时。
注意
如果指定代理被设置采用
setProxy()
, the factory will not be used.
PySide2.QtNetwork.QNetworkAccessManager.
setRedirectPolicy
(
policy
)
¶
policy
–
RedirectPolicy
Sets the manager’s redirect policy to be the
policy
specified. This policy will affect all subsequent requests created by the manager.
Use this function to enable or disable HTTP redirects on the manager’s level.
注意
当创建请求时,QNetworkRequest::RedirectAttributePolicy 拥有最高优先级,下一优先级是
FollowRedirectsAttribute
. Finally, the manager’s policy has the lowest priority.
为向后兼容,默认值为
ManualRedirectPolicy
。未来可能改变,且某些类型的自动重定向策略将变为默认;鼓励依赖手动处理重定向的客户端,在其代码中明确设置此策略。
另请参阅
redirectPolicy()
RedirectPolicy
FollowRedirectsAttribute
PySide2.QtNetwork.QNetworkAccessManager.
setStrictTransportSecurityEnabled
(
enabled
)
¶
enabled
–
bool
若
enabled
is
true
,
QNetworkAccessManager
follows the HTTP Strict Transport Security policy (HSTS, RFC6797). When processing a request,
QNetworkAccessManager
automatically replaces the “http” scheme with “https” and uses a secure transport for HSTS hosts. If it’s set explicitly, port 80 is replaced by port 443.
When HSTS is enabled, for each HTTP response containing HSTS header and received over a secure transport,
QNetworkAccessManager
will update its HSTS cache, either remembering a host with a valid policy or removing a host with an expired or disabled HSTS policy.
PySide2.QtNetwork.QNetworkAccessManager.
setTransferTimeout
(
[
timeout=QNetworkRequest.DefaultTransferTimeoutConstant
]
)
¶
timeout
–
int
集
timeout
作为传输超时 (以毫秒为单位)。
Transfers are aborted if no bytes are transferred before the timeout expires. Zero means no timer is set. If no argument is provided, the timeout is
DefaultTransferTimeoutConstant
. If this function is not called, the timeout is disabled and has the value zero. The request-specific non-zero timeouts set for the requests that are executed override this value. This means that if
QNetworkAccessManager
has an enabled timeout, it needs to be disabled to execute a request without a timeout.
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
sslErrors
(
reply
,
errors
)
¶
reply
–
QNetworkReply
errors –
PySide2.QtNetwork.QNetworkAccessManager.
strictTransportSecurityHosts
(
)
¶
Returns the list of HTTP Strict Transport Security policies. This list can differ from what was initially set via
addStrictTransportSecurityHosts()
if HSTS cache was updated from a “Strict-Transport-Security” response header.
PySide2.QtNetwork.QNetworkAccessManager.
supportedSchemes
(
)
¶
字符串列表
列出由访问管理器支持的所有 URL 方案。
PySide2.QtNetwork.QNetworkAccessManager.
supportedSchemesImplementation
(
)
¶
字符串列表
列出由访问管理器支持的所有 URL 方案。
您不应该直接调用此函数。使用
supportedSchemes()
代替。
Reimplement this slot to provide your own supported schemes in a
QNetworkAccessManager
subclass. It is for instance necessary when your subclass provides support for new protocols.
Because of binary compatibility constraints, the
supportedSchemes()
method (introduced in Qt 5.2) is not virtual. Instead,
supportedSchemes()
will dynamically detect and call this slot.
另请参阅
PySide2.QtNetwork.QNetworkAccessManager.
transferTimeout
(
)
¶
int
返回用于传输的超时 (以毫秒为单位)。
此超时为 0 若
setTransferTimeout()
hasn’t been called, which means that the timeout is not used.
另请参阅