lol
lol

Reputation: 4200

How to get the LAN IP that a socket is sending (linux)

I need some code to get the address of the socket i just created (to filter out packets originating from localhost on a multicast network)

this:

socket.gethostbyname(socket.gethostname())

works on mac but it returns only the localhost IP in linux... is there anyway to get the LAN address

thanks

--edit--

is it possible to get it from the socket settings itself, like, the OS has to select a LAN IP to send on... can i play on getsockopt(... IP_MULTICAST_IF...) i dont know exactly how to use this though...?

--- edit ---

SOLVED!

send_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)

putting this on the send socket eliminated packet echos to the host sending them, which eliminates the need for the program to know which IP the OS has selected to send.

yay!

Upvotes: 0

Views: 668

Answers (2)

Alex Martelli
Alex Martelli

Reputation: 881477

Looks like you're looking for the getsockname method of socket objects.

Upvotes: 1

mykhal
mykhal

Reputation: 19895

quick answer - socket.getpeername() (provided that socket is a socket object, not a module)

(playing around in python/ipython/idle/... interactive shell is very helpful)

.. or if I read you question carefully, maybe socket.getsockname() :)

Upvotes: 0

Related Questions