Table Of Contents

Previous topic

QSsl

Next topic

QNetworkProxyQuery

QNetworkProxy

Synopsis

Functions

Static functions

Detailed Description

The PySide.QtNetwork.QNetworkProxy class provides a network layer proxy.

PySide.QtNetwork.QNetworkProxy provides the method for configuring network layer proxy support to the Qt network classes. The currently supported classes are PySide.QtNetwork.QAbstractSocket , PySide.QtNetwork.QTcpSocket , PySide.QtNetwork.QUdpSocket , PySide.QtNetwork.QTcpServer , PySide.QtNetwork.QNetworkAccessManager and PySide.QtNetwork.QFtp . 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 QAbstractSocket.setProxy() and QTcpServer.setProxy() . 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 PySide.QtNetwork.QAbstractSocket.connectToHost() , PySide.QtNetwork.QUdpSocket.bind() or PySide.QtNetwork.QTcpServer.listen() is equivalent to QHostAddress.LocalHost or QHostAddress.LocalHostIPv6 .

Each type of proxy support has certain restrictions associated with it. You should read the QNetworkProxy.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 in 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 QNetworkProxy.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 with PySide.QtNetwork.QUdpSocket and PySide.QtNetwork.QTcpServer :

With PySide.QtNetwork.QUdpSocket , a call to PySide.QtNetwork.QUdpSocket.bind() may fail with a timeout error. If a port number other than 0 is passed to PySide.QtNetwork.QUdpSocket.bind() , it is not guaranteed that it is the specified port that will be used. Use PySide.QtNetwork.QUdpSocket.localPort() and PySide.QtNetwork.QUdpSocket.localAddress() 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 PySide.QtNetwork.QTcpServer a call to PySide.QtNetwork.QTcpServer.listen() may fail with a timeout error. If a port number other than 0 is passed to PySide.QtNetwork.QTcpServer.listen() , then it is not guaranteed that it is the specified port that will be used. Use PySide.QtNetwork.QTcpServer.serverPort() and PySide.QtNetwork.QTcpServer.serverAddress() to get the actual address and port used to listen for connections. SOCKS5 only supports one accepted connection per call to PySide.QtNetwork.QTcpServer.listen() , and each call is likely to result in a different PySide.QtNetwork.QTcpServer.serverPort() being used.

class PySide.QtNetwork.QNetworkProxy
class PySide.QtNetwork.QNetworkProxy(type[, hostName=""[, port=0[, user=""[, password=""]]]])
class PySide.QtNetwork.QNetworkProxy(other)
Parameters:

Constructs a PySide.QtNetwork.QNetworkProxy with DefaultProxy type; the proxy type is determined by PySide.QtNetwork.QNetworkProxy.applicationProxy() , which defaults to NoProxy .

Constructs a PySide.QtNetwork.QNetworkProxy with type , hostName , port , user and password .

The default capabilities for proxy type type are set automatically.

Constructs a copy of other .

PySide.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 PySide.QtNetwork.QNetworkProxy.setApplicationProxy()
QNetworkProxy.Socks5Proxy Socks5 proxying is used
QNetworkProxy.HttpProxy HTTP transparent proxying is used (This value was introduced in 4.3.)
QNetworkProxy.HttpCachingProxy Proxying for HTTP requests only (This value was introduced in 4.4.)
QNetworkProxy.FtpCachingProxy Proxying for FTP requests only (This value was introduced in 4.4.)

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 PySide.QtNetwork.QNetworkAccessManager ) CachingCapability , HostNameLookupCapability
Caching FTP Implemented using an FTP proxy, it is useful only in the context of FTP requests (see PySide.QtNetwork.QFtp , PySide.QtNetwork.QNetworkAccessManager ) CachingCapability , HostNameLookupCapability

Also note that you shouldn’t set the application default proxy ( PySide.QtNetwork.QNetworkProxy.setApplicationProxy() ) to a proxy that doesn’t have the TunnelingCapability capability. If you do, PySide.QtNetwork.QTcpSocket will not know how to open connections.

PySide.QtNetwork.QNetworkProxy.Capability

These flags indicate the capabilities that a given proxy server supports.

PySide.QtNetwork.QNetworkProxy sets different capabilities by default when the object is created (see QNetworkProxy.ProxyType for a list of the defaults). However, it is possible to change the capabitilies after the object has been created with PySide.QtNetwork.QNetworkProxy.setCapabilities() .

The capabilities that PySide.QtNetwork.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.
static PySide.QtNetwork.QNetworkProxy.applicationProxy()
Return type:PySide.QtNetwork.QNetworkProxy

Returns the application level network proxying.

If a PySide.QtNetwork.QAbstractSocket or PySide.QtNetwork.QTcpSocket has the QNetworkProxy.DefaultProxy type, then the PySide.QtNetwork.QNetworkProxy returned by this function is used.

PySide.QtNetwork.QNetworkProxy.capabilities()
Return type:PySide.QtNetwork.QNetworkProxy.Capabilities

Returns the capabilities of this proxy server.

PySide.QtNetwork.QNetworkProxy.hostName()
Return type:unicode

Returns the host name of the proxy host.

PySide.QtNetwork.QNetworkProxy.isCachingProxy()
Return type:PySide.QtCore.bool

Returns true if this proxy supports the QNetworkProxy.CachingCapability 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 PySide.QtNetwork.QNetworkProxy.setCapabilities() .

PySide.QtNetwork.QNetworkProxy.isTransparentProxy()
Return type:PySide.QtCore.bool

Returns true if this proxy supports transparent tunneling of TCP connections. This matches the QNetworkProxy.TunnelingCapability 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 PySide.QtNetwork.QNetworkProxy.setCapabilities() .

PySide.QtNetwork.QNetworkProxy.__ne__(other)
Parameters:otherPySide.QtNetwork.QNetworkProxy
Return type:PySide.QtCore.bool

Compares the value of this network proxy to other and returns true if they differ.

PySide.QtNetwork.QNetworkProxy.__eq__(other)
Parameters:otherPySide.QtNetwork.QNetworkProxy
Return type:PySide.QtCore.bool

Compares the value of this network proxy to other and returns true if they are equal (same proxy type, server as well as username and password)

PySide.QtNetwork.QNetworkProxy.password()
Return type:unicode

Returns the password used for authentication.

PySide.QtNetwork.QNetworkProxy.port()
Return type:PySide.QtCore.quint16

Returns the port of the proxy host.

static PySide.QtNetwork.QNetworkProxy.setApplicationProxy(proxy)
Parameters:proxyPySide.QtNetwork.QNetworkProxy

Sets the application level network proxying to be networkProxy .

If a PySide.QtNetwork.QAbstractSocket or PySide.QtNetwork.QTcpSocket has the QNetworkProxy.DefaultProxy type, then the PySide.QtNetwork.QNetworkProxy set with this function is used. If you want more flexibility in determining which the proxy, use the PySide.QtNetwork.QNetworkProxyFactory class.

Setting a default proxy value with this function will override the application proxy factory set with QNetworkProxyFactory::setApplicationProxyFactory.

PySide.QtNetwork.QNetworkProxy.setCapabilities(capab)
Parameters:capabPySide.QtNetwork.QNetworkProxy.Capabilities
PySide.QtNetwork.QNetworkProxy.setHostName(hostName)
Parameters:hostName – unicode

Sets the host name of the proxy host to be hostName .

PySide.QtNetwork.QNetworkProxy.setPassword(password)
Parameters:password – unicode

Sets the password for proxy authentication to be password .

PySide.QtNetwork.QNetworkProxy.setPort(port)
Parameters:portPySide.QtCore.quint16

Sets the port of the proxy host to be port .

PySide.QtNetwork.QNetworkProxy.setType(type)
Parameters:typePySide.QtNetwork.QNetworkProxy.ProxyType
PySide.QtNetwork.QNetworkProxy.setUser(userName)
Parameters:userName – unicode

Sets the user name for proxy authentication to be user .

PySide.QtNetwork.QNetworkProxy.type()
Return type:PySide.QtNetwork.QNetworkProxy.ProxyType

Returns the proxy type for this instance.

PySide.QtNetwork.QNetworkProxy.user()
Return type:unicode

Returns the user name used for authentication.