Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting IP address
#1
How, I hope this can be a complicated issue, but please assume my device has one ip adapter, one address, no layers of oranges to peel away etc!

The very documented way to get the local IP address is socket.gethostbyname(socket.gethostname())

This is all I've done with sockets before, and as I've always mostly been working on the same node I haven't noticed that this has been returning 127.0.0.1

This is ok when I'm connecting sockets on the same machine, but it ISN'T the local IP address. I need the IP address of the connected adapter.

Now, I fibbed a little earlier, this device has a wifi and hardware port, one will lock out the other, so I need the address of the one that's connected. This will ALLways be cable, unless it's not!

I don't to parse if unless I really have to.

Ideas?

Many thanks.
Reply
#2
https://nmap.org/
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Try this perhaps
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('w3.org', 80)) # or python-forum.io or perhaps a known address in your local network
ip = s.getsockname()[0]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020