NetworkAddress#
Superclasses: Object
Implemented Interfaces: SocketConnectable
GNetworkAddress provides an easy way to resolve a hostname and
then attempt to connect to that host, handling the possibility of
multiple IP addresses and multiple address families.
The enumeration results of resolved addresses may be cached as long as this object is kept alive which may have unexpected results if alive for too long.
See SocketConnectable for an example of using the connectable
interface.
Constructors#
- class NetworkAddress
- classmethod new(hostname: str, port: int) NetworkAddress#
Creates a new
SocketConnectablefor connecting to the givenhostnameandport.Note that depending on the configuration of the machine, a
hostnameoflocalhostmay refer to the IPv4 loopback address only, or to both IPv4 and IPv6; usenew_loopback()to create aNetworkAddressthat is guaranteed to resolve to both addresses.Added in version 2.22.
- Parameters:
hostname – the hostname
port – the port
- classmethod new_loopback(port: int) NetworkAddress#
Creates a new
SocketConnectablefor connecting to the local host over a loopback connection to the givenport. This is intended for use in connecting to local services which may be running on IPv4 or IPv6.The connectable will return IPv4 and IPv6 loopback addresses, regardless of how the host resolves
localhost. By contrast,new()will often only return an IPv4 address when resolvinglocalhost, and an IPv6 address forlocalhost6.get_hostname()will always returnlocalhostfor aNetworkAddresscreated with this constructor.Added in version 2.44.
- Parameters:
port – the port
Methods#
- class NetworkAddress
- get_hostname() str#
Gets
addr's hostname. This might be either UTF-8 or ASCII-encoded, depending on whataddrwas created with.Added in version 2.22.
- classmethod parse(default_port: int) NetworkAddress#
Creates a new
SocketConnectablefor connecting to the givenhostnameandport. May fail and returnNonein case parsinghost_and_portfails.host_and_portmay be in any of a number of recognised formats; an IPv6 address, an IPv4 address, or a domain name (in which case a DNS lookup is performed). Quoting with [] is supported for all address types. A port override may be specified in the usual way with a colon.If no port is specified in
host_and_portthendefault_portwill be used as the port number to connect to.In general,
host_and_portis expected to be provided by the user (allowing them to give the hostname, and a port override if necessary) anddefault_portis expected to be provided by the application.(The port component of
host_and_portcan also be specified as a service name rather than as a numeric port, but this functionality is deprecated, because it depends on the contents of /etc/services, which is generally quite sparse on platforms other than Linux.)Added in version 2.22.
- Parameters:
default_port – the default port if not in
host_and_port
- classmethod parse_uri(default_port: int) NetworkAddress#
Creates a new
SocketConnectablefor connecting to the givenuri. May fail and returnNonein case parsingurifails.Using this rather than
new()orparse()allowsSocketClientto determine when to use application-specific proxy protocols.Added in version 2.26.
- Parameters:
default_port – The default port if none is found in the URI