3

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

HBasiri
  • 343
  • 1
  • 6
  • 16

2 Answers2

1

try use this

import socket

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

print(LocalIP)
Ahmed
  • 414
  • 4
  • 3
0

Please use this. That's when you have more than one IP on your machine, you manipulate the [0, 1] to pick the correct one. In my case, it's the second one. That's why 1 but in yours its 0.

LocalIP = (socket.gethostbyname_ex(socket.gethostname())[2][1])

Documentation for socket.gethostbyname_ex(hostname)

Yahia Zakaria
  • 1,136
  • 9
  • 14
kal
  • 11