Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/airflow/utils/net.py: 36%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18from __future__ import annotations
20import socket
21from functools import lru_cache
23from airflow.configuration import conf
26# patched version of socket.getfqdn() - see https://github.com/python/cpython/issues/49254
27@lru_cache(maxsize=None)
28def getfqdn(name=""):
29 """
30 Get fully qualified domain name from name.
32 An empty argument is interpreted as meaning the local host.
33 """
34 name = name.strip()
35 if not name or name == "0.0.0.0":
36 name = socket.gethostname()
37 try:
38 addrs = socket.getaddrinfo(name, None, 0, socket.SOCK_DGRAM, 0, socket.AI_CANONNAME)
39 except OSError:
40 pass
41 else:
42 for addr in addrs:
43 if addr[3]:
44 name = addr[3]
45 break
46 return name
49def get_host_ip_address():
50 """Fetch host ip address."""
51 return socket.gethostbyname(getfqdn())
54def get_hostname():
55 """Fetch the hostname using the callable from config or use `airflow.utils.net.getfqdn` as a fallback."""
56 return conf.getimport("core", "hostname_callable", fallback="airflow.utils.net.getfqdn")()