2

I want get my ip address on my router(for example: 192.168.1.xxx i use gethostbyname() but is show 127.0.0.1

2 Answers2

1

To get the router that your PC is connected to you can try getting the IP of the default gateway that your PC is using - since your PC will most likely use the router as the default gateway.

Use netifaces for that:

import netifaces

gw = netifaces.gateways()

print(gw['default'][netifaces.AF_INET][0])

Which gives:

192.168.1.1

In my case.

rdas
  • 20,604
  • 6
  • 33
  • 46
0

This seems to stop working if you have mapping to 127.0.0.1 in your HOSTS file. Instead I will use getfqdn() (To get fully qualified domain name):

import socket
print(socket.gethostbyname(socket.getfqdn()))
Wasif
  • 14,755
  • 3
  • 14
  • 34