QNetworkProxy¶
The
QNetworkProxy
class provides a network layer proxy. More…
Synopsis¶
Functions¶
def
__eq__
(other)def
__ne__
(other)def
capabilities
()def
hasRawHeader
(headerName)def
header
(header)def
hostName
()def
isCachingProxy
()def
isTransparentProxy
()def
password
()def
port
()def
rawHeader
(headerName)def
rawHeaderList
()def
setCapabilities
(capab)def
setHeader
(header, value)def
setHostName
(hostName)def
setPassword
(password)def
setPort
(port)def
setRawHeader
(headerName, value)def
setType
(type)def
setUser
(userName)def
swap
(other)def
type
()def
user
()
Static functions¶
def
applicationProxy
()def
setApplicationProxy
(proxy)
Detailed Description¶
QNetworkProxy
provides the method for configuring network layer proxy support to the Qt network classes. The currently supported classes areQAbstractSocket
,QTcpSocket
,QUdpSocket
,QTcpServer
andQNetworkAccessManager
. The proxy support is designed to be as transparent as possible. This means that existing network-enabled applications that you have written should automatically support network proxy using the following code.proxy = QNetworkProxy() proxy.setType(QNetworkProxy.Socks5Proxy) proxy.setHostName("proxy.example.com") proxy.setPort(1080) proxy.setUser("username") proxy.setPassword("password") QNetworkProxy.setApplicationProxy(proxy)An alternative to setting an application wide proxy is to specify the proxy for individual sockets using
setProxy()
andsetProxy()
. In this way, it is possible to disable the use of a proxy for specific sockets using the following code:serverSocket.setProxy(QNetworkProxy.NoProxy)Network proxy is not used if the address used in
connectToHost()
,bind()
orlisten()
is equivalent toLocalHost
orLocalHostIPv6
.Each type of proxy support has certain restrictions associated with it. You should read the
ProxyType
documentation carefully before selecting a proxy type to use.Note
Changes made to currently connected sockets do not take effect. If you need to change a connected socket, you should reconnect it.
SOCKS5¶
The SOCKS5 support since Qt 4 is based on RFC 1928 and RFC 1929 . The supported authentication methods are no authentication and username/password authentication. Both IPv4 and IPv6 are supported. Domain names are resolved through the SOCKS5 server if the
HostNameLookupCapability
is enabled, otherwise they are resolved locally and the IP address is sent to the server. There are several things to remember when using SOCKS5 withQUdpSocket
andQTcpServer
:With
QUdpSocket
, a call tobind()
may fail with a timeout error. If a port number other than 0 is passed tobind()
, it is not guaranteed that it is the specified port that will be used. UselocalPort()
andlocalAddress()
to get the actual address and port number in use. Because proxied UDP goes through two UDP connections, it is more likely that packets will be dropped.With
QTcpServer
a call tolisten()
may fail with a timeout error. If a port number other than 0 is passed tolisten()
, then it is not guaranteed that it is the specified port that will be used. UseserverPort()
andserverAddress()
to get the actual address and port used to listen for connections. SOCKS5 only supports one accepted connection per call tolisten()
, and each call is likely to result in a differentserverPort()
being used.See also
- class PySide2.QtNetwork.QNetworkProxy¶
PySide2.QtNetwork.QNetworkProxy(type[, hostName=””[, port=0[, user=””[, password=””]]]])
PySide2.QtNetwork.QNetworkProxy(other)
- param type:
- param hostName:
str
- param password:
str
- param port:
quint16
- param other:
- param user:
str
Constructs a
QNetworkProxy
withDefaultProxy
type.The proxy type is determined by
applicationProxy()
, which defaults toNoProxy
or a system-wide proxy if one is configured.See also
Constructs a
QNetworkProxy
withtype
,hostName
,port
,user
andpassword
.The default capabilities for proxy type
type
are set automatically.See also
Constructs a copy of
other
.
- PySide2.QtNetwork.QNetworkProxy.ProxyType¶
This enum describes the types of network proxying provided in Qt.
There are two types of proxies that Qt understands: transparent proxies and caching proxies. The first group consists of proxies that can handle any arbitrary data transfer, while the second can only handle specific requests. The caching proxies only make sense for the specific classes where they can be used.
Constant
Description
QNetworkProxy.NoProxy
No proxying is used
QNetworkProxy.DefaultProxy
Proxy is determined based on the application proxy set using
setApplicationProxy()
QNetworkProxy.Socks5Proxy
Socks5
proxying is usedQNetworkProxy.HttpProxy
HTTP transparent proxying is used
QNetworkProxy.HttpCachingProxy
Proxying for HTTP requests only
QNetworkProxy.FtpCachingProxy
Proxying for FTP requests only
The table below lists different proxy types and their capabilities. Since each proxy type has different capabilities, it is important to understand them before choosing a proxy type.
Proxy type
Description
Default capabilities
SOCKS 5
Generic proxy for any kind of connection. Supports TCP, UDP, binding to a port (incoming connections) and authentication.
TunnelingCapability
,ListeningCapability
,UdpTunnelingCapability
,HostNameLookupCapability
HTTP
Implemented using the “CONNECT” command, supports only outgoing TCP connections; supports authentication.
TunnelingCapability
,CachingCapability
,HostNameLookupCapability
Caching-only HTTP
Implemented using normal HTTP commands, it is useful only in the context of HTTP requests (see
QNetworkAccessManager
)CachingCapability
,HostNameLookupCapability
Caching FTP
Implemented using an FTP proxy, it is useful only in the context of FTP requests (see
QNetworkAccessManager
)CachingCapability
,HostNameLookupCapability
Also note that you shouldn’t set the application default proxy (
setApplicationProxy()
) to a proxy that doesn’t have theTunnelingCapability
capability. If you do,QTcpSocket
will not know how to open connections.See also
- PySide2.QtNetwork.QNetworkProxy.Capability¶
These flags indicate the capabilities that a given proxy server supports.
QNetworkProxy
sets different capabilities by default when the object is created (seeProxyType
for a list of the defaults). However, it is possible to change the capabilities after the object has been created withsetCapabilities()
.The capabilities that
QNetworkProxy
supports are:Constant
Description
QNetworkProxy.TunnelingCapability
Ability to open transparent, tunneled TCP connections to a remote host. The proxy server relays the transmission verbatim from one side to the other and does no caching.
QNetworkProxy.ListeningCapability
Ability to create a listening socket and wait for an incoming TCP connection from a remote host.
QNetworkProxy.UdpTunnelingCapability
Ability to relay UDP datagrams via the proxy server to and from a remote host.
QNetworkProxy.CachingCapability
Ability to cache the contents of the transfer. This capability is specific to each protocol and proxy type. For example, HTTP proxies can cache the contents of web data transferred with “GET” commands.
QNetworkProxy.HostNameLookupCapability
Ability to connect to perform the lookup on a remote host name and connect to it, as opposed to requiring the application to perform the name lookup and request connection to IP addresses only.
QNetworkProxy.SctpTunnelingCapability
Ability to open transparent, tunneled SCTP connections to a remote host.
QNetworkProxy.SctpListeningCapability
Ability to create a listening socket and wait for an incoming SCTP connection from a remote host.
- static PySide2.QtNetwork.QNetworkProxy.applicationProxy()¶
- Return type:
Returns the application level network proxying.
If a
QAbstractSocket
orQTcpSocket
has theDefaultProxy
type, then theQNetworkProxy
returned by this function is used.
- PySide2.QtNetwork.QNetworkProxy.capabilities()¶
- Return type:
Capabilities
Returns the capabilities of this proxy server.
See also
- PySide2.QtNetwork.QNetworkProxy.hasRawHeader(headerName)¶
- Parameters:
headerName –
PySide2.QtCore.QByteArray
- Return type:
bool
Returns
true
if the raw headerheaderName
is in use for this proxy. Returnsfalse
if the proxy is not of typeHttpProxy
orHttpCachingProxy
.See also
- PySide2.QtNetwork.QNetworkProxy.header(header)¶
- Parameters:
header –
KnownHeaders
- Return type:
object
Returns the value of the known network header
header
if it is in use for this proxy. If it is not present, returns QVariant() (i.e., an invalid variant).See also
KnownHeaders
rawHeader()
setHeader()
- PySide2.QtNetwork.QNetworkProxy.hostName()¶
- Return type:
str
Returns the host name of the proxy host.
See also
- PySide2.QtNetwork.QNetworkProxy.isCachingProxy()¶
- Return type:
bool
Returns
true
if this proxy supports theCachingCapability
capability.In Qt 4.4, the capability was tied to the proxy type, but since Qt 4.5 it is possible to remove the capability of caching from a proxy by calling
setCapabilities()
.See also
- PySide2.QtNetwork.QNetworkProxy.isTransparentProxy()¶
- Return type:
bool
Returns
true
if this proxy supports transparent tunneling of TCP connections. This matches theTunnelingCapability
capability.In Qt 4.4, the capability was tied to the proxy type, but since Qt 4.5 it is possible to remove the capability of caching from a proxy by calling
setCapabilities()
.See also
- PySide2.QtNetwork.QNetworkProxy.__ne__(other)¶
- Parameters:
other –
PySide2.QtNetwork.QNetworkProxy
- Return type:
bool
Compares the value of this network proxy to
other
and returnstrue
if they differ.
- PySide2.QtNetwork.QNetworkProxy.__eq__(other)¶
- Parameters:
other –
PySide2.QtNetwork.QNetworkProxy
- Return type:
bool
Compares the value of this network proxy to
other
and returnstrue
if they are equal (same proxy type, server as well as username and password)
- PySide2.QtNetwork.QNetworkProxy.password()¶
- Return type:
str
Returns the password used for authentication.
See also
- PySide2.QtNetwork.QNetworkProxy.port()¶
- Return type:
quint16
Returns the port of the proxy host.
See also
- PySide2.QtNetwork.QNetworkProxy.rawHeader(headerName)¶
- Parameters:
headerName –
PySide2.QtCore.QByteArray
- Return type:
Returns the raw form of header
headerName
. If no such header is present or the proxy is not of typeHttpProxy
orHttpCachingProxy
, an emptyQByteArray
is returned, which may be indistinguishable from a header that is present but has no content (usehasRawHeader()
to find out if the header exists or not).Raw headers can be set with
setRawHeader()
or withsetHeader()
.See also
- PySide2.QtNetwork.QNetworkProxy.rawHeaderList()¶
- Return type:
Returns a list of all raw headers that are set in this network proxy. The list is in the order that the headers were set.
If the proxy is not of type
HttpProxy
orHttpCachingProxy
an emptyQList
is returned.See also
- static PySide2.QtNetwork.QNetworkProxy.setApplicationProxy(proxy)¶
- Parameters:
proxy –
PySide2.QtNetwork.QNetworkProxy
Sets the application level network proxying to be
networkProxy
.If a
QAbstractSocket
orQTcpSocket
has theDefaultProxy
type, then theQNetworkProxy
set with this function is used. If you want more flexibility in determining which proxy is used, use theQNetworkProxyFactory
class.Setting a default proxy value with this function will override the application proxy factory set with
setApplicationProxyFactory
, and disable the use of a system proxy.
- PySide2.QtNetwork.QNetworkProxy.setCapabilities(capab)¶
- Parameters:
capab –
Capabilities
Sets the capabilities of this proxy to
capabilities
.See also
- PySide2.QtNetwork.QNetworkProxy.setHeader(header, value)¶
- Parameters:
header –
KnownHeaders
value – object
Sets the value of the known header
header
to bevalue
, overriding any previously set headers. This operation also sets the equivalent raw HTTP header.If the proxy is not of type
HttpProxy
orHttpCachingProxy
this has no effect.See also
KnownHeaders
setRawHeader()
header()
- PySide2.QtNetwork.QNetworkProxy.setHostName(hostName)¶
- Parameters:
hostName – str
Sets the host name of the proxy host to be
hostName
.See also
- PySide2.QtNetwork.QNetworkProxy.setPassword(password)¶
- Parameters:
password – str
Sets the password for proxy authentication to be
password
.See also
- PySide2.QtNetwork.QNetworkProxy.setPort(port)¶
- Parameters:
port –
quint16
Sets the port of the proxy host to be
port
.See also
- PySide2.QtNetwork.QNetworkProxy.setRawHeader(headerName, value)¶
- Parameters:
headerName –
PySide2.QtCore.QByteArray
value –
PySide2.QtCore.QByteArray
Sets the header
headerName
to be of valueheaderValue
. IfheaderName
corresponds to a known header (seeKnownHeaders
), the raw format will be parsed and the corresponding “cooked” header will be set as well.For example:
request.setRawHeader("Last-Modified", "Sun, 06 Nov 1994 08:49:37 GMT")
will also set the known header LastModifiedHeader to be the
QDateTime
object of the parsed date.Note
Setting the same header twice overrides the previous setting. To accomplish the behaviour of multiple HTTP headers of the same name, you should concatenate the two values, separating them with a comma (“,”) and set one single raw header.
If the proxy is not of type
HttpProxy
orHttpCachingProxy
this has no effect.See also
KnownHeaders
setHeader()
hasRawHeader()
rawHeader()
- PySide2.QtNetwork.QNetworkProxy.setType(type)¶
- Parameters:
type –
ProxyType
Sets the proxy type for this instance to be
type
.Note that changing the type of a proxy does not change the set of capabilities this
QNetworkProxy
object holds if any capabilities have been set withsetCapabilities()
.See also
- PySide2.QtNetwork.QNetworkProxy.setUser(userName)¶
- Parameters:
userName – str
Sets the user name for proxy authentication to be
user
.See also
- PySide2.QtNetwork.QNetworkProxy.swap(other)¶
- Parameters:
other –
PySide2.QtNetwork.QNetworkProxy
Swaps this network proxy instance with
other
. This function is very fast and never fails.
- PySide2.QtNetwork.QNetworkProxy.type()¶
- Return type:
Returns the proxy type for this instance.
See also
- PySide2.QtNetwork.QNetworkProxy.user()¶
- Return type:
str
Returns the user name used for authentication.
See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.