Module mfutil.net
Utility functions around network.
Functions
def get_domainname(use_resolv_conf=True, resolv_conf_file='/etc/resolv.conf')
-
Get the domain name of the server.
The domain name does not include the hostname.
We try first with the getfqdn method then with the resolv.conf file because the domain can be found here.
Args
use_resolv_conf
:boolean
- if set to True, we use the resolv.conf file.
resolv_conf_file
:string
- full path of the resolv.conf file (useful for unit testing).
Returns
string
- the domain name of the server (or None if we can't find it)
def get_full_hostname(use_resolv_conf=True, resolv_conf_file='/etc/resolv.conf')
-
Return the "full" hostname of the server.
"full" hostname means "with network domain" appended.
Args
use_resolv_conf
:boolean
- if set to True, we use the resolv.conf file to find the domain name.
resolv_conf_file
:string
- full path of the resolv.conf file (useful for unit testing).
Returns
string
- the "full" hostname of the server.
def get_ip_for_hostname(hostname, ignore_hostnames_list=['AUTO'])
-
Get the IP of a given hostname.
Args
hostname
:string
- hostname to find.
ignore_hostnames_list
:list
ofstrings
- special hostname values which won't be lookup ip (if the given hostname is in this list, this function will return the given hostname without any modification).
Returns
(string) IP of the given hostname (or None if we can't find it).
def get_real_ip()
-
Try to find and return the real IP of the server.
We try to avoid to return 127.0.0.1 by examining network interfaces.
Returns
string
- the real IP of the server.
def get_simple_hostname()
-
Return the "simple" hostname of the server.
"simple" hostname means "without network domain", so without any dot in the hostname.
Returns
string
- the "simple" hostname of the server.
def ping_tcp_port(host, port, timeout=5)
-
Ping a TCP host/port with a configurable timeout.
It's not really a ping but a little connection attempt to see if the port is open and listened. The timeout is useful when there is a kind of firewall between.
No Exception are raised in any case.
Args
host
:string
- the hostname/ip to ping.
port
:int
- the TCP port to ping.
timeout
:int
- timeout in seconds.
Returns
boolean
- True if the port is open and listened.