ok

Mini Shell

Direktori : /var/opt/nydus/ops/primordial/
Upload File :
Current File : //var/opt/nydus/ops/primordial/constants.py

# -*- coding: utf-8 -*-

"""This module holds constants needed for primordial."""

try:
    import fcntl
except ImportError:
    pass  # Windows
import ipaddress
import struct
import socket


CANONICAL_TIMESTRING_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
"Standard format for time strings."
CANONICAL_TIMESTRING_FORMAT_NO_ZULU = '%Y-%m-%dT%H:%M:%S.%f'
"Standard format for time strings without a trailing Z."
CANONICAL_TIMESTRING_FORMAT_NO_MICROSECONDS = '%Y-%m-%dT%H:%M:%SZ'
"Strandard format for time strings without microseconds."


def getIP() -> str:
    """Get the IP address of this machine."""
    try:
        ips = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")]
        if ips:
            for ip in ips:
                if ipaddress.ip_address(str(ip)).is_private:
                    return ip
            return ips[0]
    except socket.gaierror:
        pass

    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        ipstr = struct.pack('256s'.encode('ascii'), 'eth0'.encode('ascii'))
        return socket.inet_ntoa(
            fcntl.ioctl(s.fileno(), 0x8915, ipstr)[20:24])
    except (IOError, RuntimeError, socket.gaierror):
        return '127.0.0.1'


IP = getIP()
"The IP address of this machine on which this is running."

Zerion Mini Shell 1.0