i test socket.gethostname() the result is ‘ddim.local’, ddim is my username. socket.getaddrinfo(socket.gethostname(), None) is report error. my python verison is 3.11.4.
相關討論: [Errno 8] nodename nor servname provided, or not known (with macOS Sierra)
https://stackoverflow.com/questions/39970606/gaierror-errno-8-nodename-nor-servname-provided-or-not-known-with-macos-sie
socket.gethostbyname(socket.gethostname()) worked well on OS X El Capitan. However, it’s not working now after the Mac updated to macOS Sierra.
Thanks!
import socket
socket.gethostbyname(socket.gethostname())
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
socket.gethostbyname(socket.gethostname())
gaierror: [Errno 8] nodename nor servname provided, or not known
socket.gethostname() 讀取/etc/hostname中的name
socket.gethostbyname(name)是通過name來提取/etc/hosts裏和name對應的ip 。
如果hosts裏沒有對應的name,會拋出錯誤。
解決方式:
打開hosts文件加入hostname名稱即可