views:

69

answers:

2

Hello,

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!

A: 

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() :)

mykhal
thanks :) -- the socket.getsockname one doesnt work on linux (because of the /etc/hosts thing) and peername requires the endpoint to be connected and seeing as i am using network multicasting it is 'never' connected (its only connected for a short time within another function)
lol
+1  A: 

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

Alex Martelli
get sock name returns the addresses from /etc/hosts -- they are usually 'localhost'
lol