Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket IO returns 127.0.0.1 as host address and not 192.168.0.* on my device

When I run the following code to determine my device's local IP address, I get 127.0.0.1 instead of 192.168.0.101.

import socket
import threading

PORT = 8080
HOST_NAME = socket.gethostname()
print(HOST_NAME)
SERVER = socket.gethostbyname(HOST_NAME)

print(SERVER)

The output i get on the console is

MyDeviceName.local
127.0.0.1
like image 955
Vasudeva S Avatar asked Oct 31 '25 10:10

Vasudeva S


1 Answers

127.0.0.1 is localhost address, it is right. If you want your device's address do this:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print(s.getsockname()[0])
like image 187
dod0lp Avatar answered Nov 03 '25 01:11

dod0lp



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!