开发者

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 eliminat开发者_如何学运维ed 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!


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜