PySide6.QtNetwork.QHostAddress¶
- class QHostAddress¶
- The - QHostAddressclass provides an IP address.- Details- This class holds an IPv4 or IPv6 address in a platform- and protocol-independent manner. - QHostAddressis normally used with the- QTcpSocket,- QTcpServer, and- QUdpSocketto 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 - QHostAddressdoes not do DNS lookups.- QHostInfois needed for that.- The class also supports common predefined addresses: - Null,- LocalHost,- LocalHostIPv6,- Broadcast, and- Any.- See also - Synopsis¶- Methods¶- def - __init__()
- def - clear()
- def - isBroadcast()
- def - isEqual()
- def - isGlobal()
- def - isInSubnet()
- def - isLinkLocal()
- def - isLoopback()
- def - isMulticast()
- def - isNull()
- def - isPrivateUse()
- def - isSiteLocal()
- def - __ne__()
- def - operator=()
- def - __eq__()
- def - protocol()
- def - scopeId()
- def - setAddress()
- def - setScopeId()
- def - swap()
- def - toIPv4Address()
- def - toIPv6Address()
- def - toString()
 - Static functions¶- def - parseSubnet()
 - 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 - class SpecialAddress¶
- Constant - Description - QHostAddress.SpecialAddress.Null - The null address object. Equivalent to - QHostAddress(). See also- isNull().- QHostAddress.SpecialAddress.LocalHost - The IPv4 localhost address. Equivalent to - QHostAddress(“127.0.0.1”).- QHostAddress.SpecialAddress.LocalHostIPv6 - The IPv6 localhost address. Equivalent to - QHostAddress(“::1”).- QHostAddress.SpecialAddress.Broadcast - The IPv4 broadcast address. Equivalent to - QHostAddress(“255.255.255.255”).- QHostAddress.SpecialAddress.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.SpecialAddress.AnyIPv6 - The IPv6 any-address. Equivalent to - QHostAddress(“::”). A socket bound with this address will listen only on IPv6 interfaces.- QHostAddress.SpecialAddress.Any - The dual stack any-address. A socket bound with this address will listen on both IPv4 and IPv6 interfaces. 
 - class ConversionModeFlag¶
- Constant - Description - QHostAddress.ConversionModeFlag.StrictConversion - (inherits - enum.Flag) Don’t convert IPv6 addresses to IPv4 when comparing two- QHostAddressobjects of different protocols, so they will always be considered different.- QHostAddress.ConversionModeFlag.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.ConversionModeFlag.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.ConversionModeFlag.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.ConversionModeFlag.ConvertUnspecifiedAddress - All unspecified addresses will compare equal, namely - AnyIPv4,- AnyIPv6and Any.- QHostAddress.ConversionModeFlag.TolerantConversion - Sets all three preceding flags. - See also 
 - PySide6.QtNetwork.QHostAddress.IPv4Protocol¶
 - PySide6.QtNetwork.QHostAddress.IPv6Protocol¶
 - PySide6.QtNetwork.QHostAddress.AnyIPProtocol¶
 - PySide6.QtNetwork.QHostAddress.UnknownNetworkLayerProtocol¶
 - __init__()¶
 - Constructs a null host address object, i.e. an address which is not valid for any host or interface. - See also - __init__(address)
- Parameters:
- address – - SpecialAddress
 
 - Constructs a - QHostAddressobject for- address.- __init__(copy)
- Parameters:
- copy – - QHostAddress
 
 - Constructs a copy of the given - address.- __init__(ip6Addr)
- Parameters:
- ip6Addr – - QIPv6Address
 
 - __init__(address)
- Parameters:
- address – str 
 
 - Constructs an IPv4 or IPv6 address based on the string - address(e.g., “127.0.0.1”).- See also - __init__(ip4Addr)
- Parameters:
- ip4Addr – int 
 
 - Constructs a host address object with the IPv4 address - ip4Addr.- clear()¶
 - Sets the host address to null and sets the protocol to - UnknownNetworkLayerProtocol.- See also - isBroadcast()¶
- Return type:
- bool 
 
 - Returns - trueif the address is the IPv4 broadcast address,- falseotherwise. 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 - QNetworkInterfaceto obtain the broadcast addresses of the local machine.- isEqual(address[, mode=QHostAddress.ConversionModeFlag.TolerantConversion])¶
- Parameters:
- address – - QHostAddress
- mode – Combination of - ConversionModeFlag
 
- Return type:
- bool 
 
 - Returns - trueif this host address is the same as the- otheraddress given; otherwise returns- false.- The parameter - modecontrols which conversions are performed between addresses of differing protocols. If no- modeis given,- TolerantConversionis performed by default.- See also - ConversionMode- operator==()- isGlobal()¶
- Return type:
- bool 
 
 - Returns - trueif the address is an IPv4 or IPv6 global address,- falseotherwise. 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. - isInSubnet(subnet)¶
- Parameters:
- subnet – .std.pairQHostAddress,int 
- Return type:
- bool 
 
 - Returns - trueif this IP is in the subnet described by- subnet. The- QHostAddressmember of- subnetcontains the network prefix and the int (second) member contains the netmask (prefix length).- isInSubnet(subnet, netmask)
- Parameters:
- subnet – - QHostAddress
- netmask – int 
 
- Return type:
- bool 
 
 - Returns - trueif this IP is in the subnet described by the network prefix- subnetand 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 - subnetargument 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 - isLinkLocal()¶
- Return type:
- bool 
 
 - Returns - trueif the address is an IPv4 or IPv6 link-local address,- falseotherwise.- 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. - isLoopback()¶
- Return type:
- bool 
 
 - returns - trueif the address is the IPv6 loopback address, or any of the IPv4 loopback addresses.- isMulticast()¶
- Return type:
- bool 
 
 - Returns - trueif the address is an IPv4 or IPv6 multicast address,- falseotherwise.- isNull()¶
- Return type:
- bool 
 
 - Returns - trueif this host address is not valid for any host or interface.- The default constructor creates a null address. - See also - isPrivateUse()¶
- Return type:
- bool 
 
 - Returns - trueif the address is an IPv6 unique local unicast address or IPv4 address reserved for local networks by RFC 1918,- falseotherwise.- isSiteLocal()¶
- Return type:
- bool 
 
 - Returns - trueif the address is an IPv6 site-local address,- falseotherwise.- 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).- isUniqueLocalUnicast()¶
- Return type:
- bool 
 
 - Returns - trueif the address is an IPv6 unique local unicast address,- falseotherwise.- 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. - __ne__(address)¶
- Parameters:
- address – - SpecialAddress
- Return type:
- bool 
 
 - Returns - trueif this host address is not the same as the- otheraddress given; otherwise returns- false.- __ne__(address)
- Parameters:
- address – - QHostAddress
- Return type:
- bool 
 
 - Returns - trueif this host address is not the same as the- otheraddress given; otherwise returns- false.- operator=(address)
- Parameters:
- address – - SpecialAddress
- Return type:
 
 - Assigns the special address - addressto this object, and returns a reference to this object.- See also - __eq__(address)¶
- Parameters:
- address – - SpecialAddress
- Return type:
- bool 
 
 - Returns - trueif this host address is the same as the- otheraddress given; otherwise returns- false.- __eq__(address)
- Parameters:
- address – - QHostAddress
- Return type:
- bool 
 
 - Returns - trueif this host address is the same as the- otheraddress given; otherwise returns- false. This operator just calls- isEqual(other,- StrictConversion).- See also - static parseSubnet(subnet)¶
- Parameters:
- subnet – str 
- Return type:
- .std.pairQHostAddress,int 
 
 - Parses the IP and subnet information contained in - subnetand 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 - protocol()¶
- Return type:
 
 - Returns the network layer protocol of the host address. - 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”). - setAddress(address)¶
- Parameters:
- address – - SpecialAddress
 
 - Sets the special address specified by - address.- setAddress(ip6Addr)
- Parameters:
- ip6Addr – - QIPv6Address
 
 - setAddress(address)
- Parameters:
- address – str 
- Return type:
- bool 
 
 - Sets the IPv4 or IPv6 address specified by the string representation specified by - address(e.g. “127.0.0.1”). Returns- trueand sets the address if the address was successfully parsed; otherwise returns- false.- setAddress(ip4Addr)
- Parameters:
- ip4Addr – int 
 
 - Set the IPv4 address specified by - ip4Addr.- 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- idis an interface name, QtNetwork will convert to an interface index using- interfaceIndexFromName()before calling the operating system networking functions.- See also - swap(other)¶
- Parameters:
- other – - QHostAddress
 
 - Swaps this host address with - other. This operation is very fast and never fails.- 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,- okwill be set to true. Otherwise, it will be set to false.- See also - toIPv6Address()¶
- Return type:
 
 - 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 as an IPv4 mapped IPv6 address. (RFC4291)- See also - 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