之前在 Windows/Mac OS 取得 ip address 透過
import socket print socket.gethostbyname(socket.gethostname())
都沒問題。但在 Linux 裡出問題了。
print socket.gethostbyname_ex(socket.gethostname())
完全沒取到 ip,
替代解法是:
from subprocess import check_output ipAddr = check_output(["hostname", "-I"])
但這段 code 在 Mac OS 上跑會出錯:
ipAddr = check_output(["hostname", "-I"]) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 573, in check_output raise CalledProcessError(retcode, cmd, output=output) subprocess.CalledProcessError: Command '['hostname', '-I']' returned non-zero exit status 1