10 examples of 'python get hostname' in Python

Every line of 'python get hostname' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
7def hostname():
8 """Get hostname."""
9 return socket.gethostname()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

38def resolve_hostname(hostname):
39 return socket.gethostbyname(hostname)
23def get_ip():
24 try:
25 hostname = socket.getfqdn(socket.gethostname())
26 ipaddr = socket.gethostbyname(hostname)
27 except Exception as msg:
28 print(msg)
29 ipaddr = ''
30 return ipaddr
102def GetFQDN():
103 return socket.getfqdn()
35def query_remote_hostname():
36 """
37 Get the remote hostname
38 """
39 import socket
40 return socket.gethostname()
46def GetHostInfo():
47 """Get information about the host."""
48 hostname = GetFQDN()
49 if hostname in BAD_HOSTNAMES:
50 raise AssertionError('Hostname cannot be one of %s' % ','.join(BAD_HOSTNAMES))
51 ipaddr = GetIP(hostname)
52 host_check, host_aliases, _ = socket.gethostbyaddr(ipaddr)
53 return hostname, ipaddr, host_aliases + [host_check]
125def getHostName():
126 '''
127 Returns current host name
128 In fact, it's a wrapper for socket.gethostname()
129 '''
130 hostname = socket.gethostname()
131 if 'win32' in sys.platform:
132 hostname = hostname.decode(sys_fs_enc)
133
134 hostname = six.text_type(hostname)
135 logger.info('Hostname: {}'.format(hostname))
136 return hostname
44def get_host_by_addr(self):
45 '''Return a triple (hostname, aliaslist, ipaddrlist) - https://docs.python.org/2/library/socket.html#socket.gethostbyaddr'''
46 try:
47 return socket.gethostbyaddr(str(self.ip))
48 except Exception as e:
49 raise e
37def get_host_by_name(self):
38 try:
39 return str(dns.resolver.query(self.names[0])[0])
40 except Exception as e:
41 raise e
38@property
39def hostname(self):
40 with self.open(_NETFILE) as fhandler:
41 for line in fhandler.read().splitlines():
42 attr, value = line.split('=')
43 if attr == 'HOSTNAME':
44 return value

Related snippets