Python: Get IP address of local machine

Posted in :

source code:

import socket
print([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] 
if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), 
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, 
socket.SOCK_DGRAM)]][0][1]]) if l][0][0])

執行結果:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *