直接上代码吧:
>>> import socket
>>> host_name = socket.gethostname()
>>> host_name
'BDSHYF000148336'
>>> socket.gethostbyname(host_name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
>>> host_name
'BDSHYF000148336'
>>> socket.gethostbyname(socket.gethostname())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
>>> socket.gethostbyname("localhost")
'127.0.0.1'
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
是因为需要把ip地址写入hosts。我这里做了个偷懒的方法,直接看本地localhost
了。