aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/webui/utils.py
blob: 1cea69f2fd1a01aed2a25e2b3b10abb20c06211b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (c) 2016 Weitian LI <liweitianux@live.com>
# MIT license

"""
Utilities for the Web UI
"""


from urllib.parse import urlparse
import socket


def get_host_ip(url):
    """
    This function parses the input URL to get the hostname (or an IP),
    then the hostname is further resolved to its IP address.

    Parameters
    ----------
    url : str
        An URL string, which generally has the following format:
        ``scheme://netloc/path;parameters?query#fragment``
        while the ``netloc`` may look like ``user:pass@example.com:port``

    Returns
    -------
    ip : str
        An IPv4 address string.
        If something wrong happens (e.g., ``gaierror``), then ``None``
        is returned.
    """
    netloc = urlparse(url).netloc
    hostname = netloc.split("@")[-1].split(":")[0]
    try:
        ip = socket.gethostbyname(hostname)
    except socket.gaierror:
        ip = None
    return ip


def get_local_ip(host="localhost", timeout=3.0):
    """
    Get the local IP address of this machine where this script runs.

    A dummy socket will be created and connects to the given host, then
    the valid local IP address used in this connection can be obtained.

    Parameters
    ----------
    host : str
        The host to which will be connected by a dummy socket, in order
        to determine the valid IP address.
    timeout : float
        Timeout (in seconds) on the blocking socket operations (e.g.,
        ``connect()``)

    Returns
    -------
    ip : str
        The local IPv4 address of this machine as a string.
        If something wrong happens (e.g., ``gaierror``), then ``None``
        is returned.
    """
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        try:
            s.settimeout(timeout)
            # Use 0 as the port will let OS determine the free port
            s.connect((host, 0))
            ip = s.getsockname()[0]
        except (socket.gaierror, socket.timeout):
            ip = None
    return ip