Python gets the Windows or Linux hostname common function share
- 2020-04-02 14:24:14
- OfStack
A generic function that gets the Windows or Linux hostname from the python OS module.
#!/usr/bin/env python
#coding=utf-8
import os
def hostname():
sys = os.name
if sys == 'nt':
hostname = os.getenv('computername')
return hostname
elif sys == 'posix':
host = os.popen('echo $HOSTNAME')
try:
hostname = host.read()
return hostname
finally:
host.close()
else:
return 'Unkwon hostname'