Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting wrong local IP address in python

My OS is Windows 8.1 and i have Windows 7 and linux debian installed in Vmware. In python (2.7) when i try to get local ip address it shows vmware's ip address(Win7) instead of Win8.1 ip address(picture)! What is the problem?
I have set vmware's network adapter to NAT.

Edit: The code i used: socket.gethostbyname(socket.gethostname())

enter image description here

like image 660
HBasiri Avatar asked Jul 04 '26 22:07

HBasiri


1 Answers

try use this

import socket

LocalIP = ''.join(socket.gethostbyname_ex(socket.gethostname())[2])

print(LocalIP)
like image 116
Ahmed Avatar answered Jul 07 '26 11:07

Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!