QHostAddress#

The QHostAddress class provides an IP address. More

Inheritance diagram of PySide6.QtNetwork.QHostAddress

Synopsis#

Functions#

Static functions#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

This class holds an IPv4 or IPv6 address in a platform- and protocol-independent manner.

QHostAddress is normally used with the QTcpSocket , QTcpServer , and QUdpSocket to connect to a host or to set up a server.

A host address is set with setAddress() , and retrieved with toIPv4Address() , toIPv6Address() , or toString() . You can check the type with protocol() .

Note

Please note that QHostAddress does not do DNS lookups. QHostInfo is needed for that.

The class also supports common predefined addresses: Null , LocalHost , LocalHostIPv6 , Broadcast , and Any .

class PySide6.QtNetwork.QHostAddress#

PySide6.QtNetwork.QHostAddress(address)

PySide6.QtNetwork.QHostAddress(copy)

PySide6.QtNetwork.QHostAddress(ip6Addr)

PySide6.QtNetwork.QHostAddress(address)

PySide6.QtNetwork.QHostAddress(ip4Addr)

Parameters:

Constructs a null host address object, i.e. an address which is not valid for any host or interface.

See also

clear()

Constructs a QHostAddress object for address.

Constructs a copy of the given address.

Constructs an IPv4 or IPv6 address based on the string address (e.g., “127.0.0.1”).

See also

setAddress()

Constructs a host address object with the IPv4 address ip4Addr.

PySide6.QtNetwork.QHostAddress.SpecialAddress#

Constant

Description

QHostAddress.Null

The null address object. Equivalent to QHostAddress() . See also isNull() .

QHostAddress.LocalHost

The IPv4 localhost address. Equivalent to QHostAddress (“127.0.0.1”).

QHostAddress.LocalHostIPv6

The IPv6 localhost address. Equivalent to QHostAddress (“::1”).

QHostAddress.Broadcast

The IPv4 broadcast address. Equivalent to QHostAddress (“255.255.255.255”).

QHostAddress.AnyIPv4

The IPv4 any-address. Equivalent to QHostAddress (“0.0.0.0”). A socket bound with this address will listen only on IPv4 interfaces.

QHostAddress.AnyIPv6

The IPv6 any-address. Equivalent to QHostAddress (“::”). A socket bound with this address will listen only on IPv6 interfaces.

QHostAddress.Any

The dual stack any-address. A socket bound with this address will listen on both IPv4 and IPv6 interfaces.

PySide6.QtNetwork.QHostAddress.ConversionModeFlag#

Constant

Description

QHostAddress.StrictConversion

(inherits enum.Flag) Don’t convert IPv6 addresses to IPv4 when comparing two QHostAddress objects of different protocols, so they will always be considered different.

QHostAddress.ConvertV4MappedToIPv4

Convert IPv4-mapped IPv6 addresses (RFC 4291 sect. 2.5.5.2) when comparing. Therefore QHostAddress (“::ffff:192.168.1.1”) will compare equal to QHostAddress (“192.168.1.1”).

QHostAddress.ConvertV4CompatToIPv4

Convert IPv4-compatible IPv6 addresses (RFC 4291 sect. 2.5.5.1) when comparing. Therefore QHostAddress (“::192.168.1.1”) will compare equal to QHostAddress (“192.168.1.1”).

QHostAddress.ConvertLocalHost

Convert the IPv6 loopback addresses to its IPv4 equivalent when comparing. Therefore e.g. QHostAddress (“::1”) will compare equal to QHostAddress (“127.0.0.1”).

QHostAddress.ConvertUnspecifiedAddress

All unspecified addresses will compare equal, namely AnyIPv4 , AnyIPv6 and Any.

QHostAddress.TolerantConversion

Sets all three preceding flags.

See also

isEqual()

PySide6.QtNetwork.QHostAddress.IPv4Protocol#
PySide6.QtNetwork.QHostAddress.IPv6Protocol#
PySide6.QtNetwork.QHostAddress.AnyIPProtocol#
PySide6.QtNetwork.QHostAddress.UnknownNetworkLayerProtocol#
PySide6.QtNetwork.QHostAddress.clear()#

Sets the host address to null and sets the protocol to UnknownNetworkLayerProtocol .

See also

Null

PySide6.QtNetwork.QHostAddress.isBroadcast()#
Return type:

bool

Returns true if the address is the IPv4 broadcast address, false otherwise. The IPv4 broadcast address is 255.255.255.255.

Note that this function does not return true for an IPv4 network’s local broadcast address. For that, please use QNetworkInterface to obtain the broadcast addresses of the local machine.

PySide6.QtNetwork.QHostAddress.isEqual(address[, mode=QHostAddress.ConversionModeFlag.TolerantConversion])#
Parameters:
Return type:

bool

Returns true if this host address is the same as the other address given; otherwise returns false.

The parameter mode controls which conversions are performed between addresses of differing protocols. If no mode is given, TolerantConversion is performed by default.

See also

ConversionMode operator==()

PySide6.QtNetwork.QHostAddress.isGlobal()#
Return type:

bool

Returns true if the address is an IPv4 or IPv6 global address, false otherwise. A global address is an address that is not reserved for special purposes (like loopback or multicast) or future purposes.

Note that IPv6 unique local unicast addresses are considered global addresses (see isUniqueLocalUnicast() ), as are IPv4 addresses reserved for local networks by RFC 1918 .

Also note that IPv6 site-local addresses are deprecated and should be considered as global in new applications. This function returns true for site-local addresses too.

PySide6.QtNetwork.QHostAddress.isInSubnet(subnet, netmask)#
Parameters:
Return type:

bool

Returns true if this IP is in the subnet described by the network prefix subnet and netmask netmask.

An IP is considered to belong to a subnet if it is contained between the lowest and the highest address in that subnet. In the case of IP version 4, the lowest address is the network address, while the highest address is the broadcast address.

The subnet argument does not have to be the actual network address (the lowest address in the subnet). It can be any valid IP belonging to that subnet. In particular, if it is equal to the IP address held by this object, this function will always return true (provided the netmask is a valid value).

See also

parseSubnet()

PySide6.QtNetwork.QHostAddress.isInSubnet(subnet)
Parameters:

subnet

Return type:

bool

This is an overloaded function.

Returns true if this IP is in the subnet described by subnet. The QHostAddress member of subnet contains the network prefix and the int (second) member contains the netmask (prefix length).

PySide6.QtNetwork.QHostAddress.isLinkLocal()#
Return type:

bool

Returns true if the address is an IPv4 or IPv6 link-local address, false otherwise.

An IPv4 link-local address is an address in the network 169.254.0.0/16. An IPv6 link-local address is one in the network fe80::/10. See the IANA IPv6 Address Space registry for more information.

PySide6.QtNetwork.QHostAddress.isLoopback()#
Return type:

bool

returns true if the address is the IPv6 loopback address, or any of the IPv4 loopback addresses.

PySide6.QtNetwork.QHostAddress.isMulticast()#
Return type:

bool

Returns true if the address is an IPv4 or IPv6 multicast address, false otherwise.

PySide6.QtNetwork.QHostAddress.isNull()#
Return type:

bool

Returns true if this host address is not valid for any host or interface.

The default constructor creates a null address.

See also

Null

PySide6.QtNetwork.QHostAddress.isSiteLocal()#
Return type:

bool

Returns true if the address is an IPv6 site-local address, false otherwise.

An IPv6 site-local address is one in the network fec0::/10. See the IANA IPv6 Address Space registry for more information.

IPv6 site-local addresses are deprecated and should not be depended upon in new applications. New applications should not depend on this function and should consider site-local addresses the same as global (which is why isGlobal() also returns true). Site-local addresses were replaced by Unique Local Addresses (ULA).

PySide6.QtNetwork.QHostAddress.isUniqueLocalUnicast()#
Return type:

bool

Returns true if the address is an IPv6 unique local unicast address, false otherwise.

An IPv6 unique local unicast address is one in the network fc00::/7. See the IANA IPv6 Address Space registry for more information.

Note that Unique local unicast addresses count as global addresses too. RFC 4193 says that, in practice, “applications may treat these addresses like global scoped addresses.” Only routers need care about the distinction.

PySide6.QtNetwork.QHostAddress.__ne__(address)#
Parameters:

addressSpecialAddress

Return type:

bool

Returns true if this host address is not the same as the other address given; otherwise returns false.

PySide6.QtNetwork.QHostAddress.__ne__(address)
Parameters:

addressPySide6.QtNetwork.QHostAddress

Return type:

bool

Returns true if this host address is not the same as the other address given; otherwise returns false.

PySide6.QtNetwork.QHostAddress.__eq__(address)#
Parameters:

addressSpecialAddress

Return type:

bool

Returns true if this host address is the same as the other address given; otherwise returns false.

PySide6.QtNetwork.QHostAddress.__eq__(address)
Parameters:

addressPySide6.QtNetwork.QHostAddress

Return type:

bool

Returns true if this host address is the same as the other address given; otherwise returns false. This operator just calls isEqual (other, StrictConversion ).

See also

isEqual()

static PySide6.QtNetwork.QHostAddress.parseSubnet(subnet)#
Parameters:

subnet – str

Parses the IP and subnet information contained in subnet and returns the network prefix for that network and its prefix length.

The IP address and the netmask must be separated by a slash (/).

This function supports arguments in the form:

  • 123.123.123.123/n where n is any value between 0 and 32

  • 123.123.123.123/255.255.255.255

  • <ipv6-address>/n where n is any value between 0 and 128

For IP version 4, this function accepts as well missing trailing components (i.e., less than 4 octets, like “192.168.1”), followed or not by a dot. If the netmask is also missing in that case, it is set to the number of octets actually passed (in the example above, it would be 24, for 3 octets).

See also

isInSubnet()

PySide6.QtNetwork.QHostAddress.protocol()#
Return type:

NetworkLayerProtocol

Returns the network layer protocol of the host address.

PySide6.QtNetwork.QHostAddress.scopeId()#
Return type:

str

Returns the scope ID of an IPv6 address. For IPv4 addresses, or if the address does not contain a scope ID, an empty QString is returned.

The IPv6 scope ID specifies the scope of reachability for non-global IPv6 addresses, limiting the area in which the address can be used. All IPv6 addresses are associated with such a reachability scope. The scope ID is used to disambiguate addresses that are not guaranteed to be globally unique.

IPv6 specifies the following four levels of reachability:

  • Node-local: Addresses that are only used for communicating with services on the same interface (e.g., the loopback interface “::1”).

  • Link-local: Addresses that are local to the network interface (link). There is always one link-local address for each IPv6 interface on your host. Link-local addresses (“fe80…”) are generated from the MAC address of the local network adaptor, and are not guaranteed to be unique.

  • Global: For globally routable addresses, such as public servers on the Internet.

When using a link-local or site-local address for IPv6 connections, you must specify the scope ID. The scope ID for a link-local address is usually the same as the interface name (e.g., “eth0”, “en1”) or number (e.g., “1”, “2”).

PySide6.QtNetwork.QHostAddress.setAddress(address)#
Parameters:

addressSpecialAddress

This is an overloaded function.

Sets the special address specified by address.

PySide6.QtNetwork.QHostAddress.setAddress(ip6Addr)
Parameters:

ip6AddrPySide6.QtNetwork.QIPv6Address

PySide6.QtNetwork.QHostAddress.setAddress(address)
Parameters:

address – str

Return type:

bool

This is an overloaded function.

Sets the IPv4 or IPv6 address specified by the string representation specified by address (e.g. “127.0.0.1”). Returns true and sets the address if the address was successfully parsed; otherwise returns false.

PySide6.QtNetwork.QHostAddress.setAddress(ip4Addr)
Parameters:

ip4Addr – int

Set the IPv4 address specified by ip4Addr.

PySide6.QtNetwork.QHostAddress.setScopeId(id)#
Parameters:

id – str

Sets the IPv6 scope ID of the address to id. If the address protocol is not IPv6, this function does nothing. The scope ID may be set as an interface name (such as “eth0” or “en1”) or as an integer representing the interface index. If id is an interface name, QtNetwork will convert to an interface index using interfaceIndexFromName() before calling the operating system networking functions.

PySide6.QtNetwork.QHostAddress.swap(other)#
Parameters:

otherPySide6.QtNetwork.QHostAddress

Swaps this host address with other. This operation is very fast and never fails.

PySide6.QtNetwork.QHostAddress.toIPv4Address([ok=None])#
Parameters:

ok – bool

Return type:

int

Returns the IPv4 address as a number.

For example, if the address is 127.0.0.1, the returned value is 2130706433 (i.e. 0x7f000001).

This value is valid if the protocol() is IPv4Protocol , or if the protocol is IPv6Protocol , and the IPv6 address is an IPv4 mapped address (RFC4291). In those cases, ok will be set to true. Otherwise, it will be set to false.

See also

toString()

PySide6.QtNetwork.QHostAddress.toIPv6Address()#
Return type:

PySide6.QtNetwork.QIPv6Address

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Returns the IPv6 address as a Q_IPV6ADDR structure. The structure consists of 16 unsigned characters.

Q_IPV6ADDR addr = hostAddr.toIPv6Address()
# addr contains 16 unsigned characters
for i in range(0, 16):
    # process addr[i]

This value is valid if the protocol() is IPv6Protocol . If the protocol is IPv4Protocol , then the address is returned an an IPv4 mapped IPv6 address. (RFC4291)

See also

toString()

PySide6.QtNetwork.QHostAddress.toString()#
Return type:

str

Returns the address as a string.

For example, if the address is the IPv4 address 127.0.0.1, the returned string is “127.0.0.1”. For IPv6 the string format will follow the RFC5952 recommendation. For Any , its IPv4 address will be returned (“0.0.0.0”)

See also

toIPv4Address()