Passing argument to setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, intr)

Richard Taylor rjt-usenet at thegrindstone.me.uk
Wed Apr 21 08:22:41 EDT 2004


Richard Taylor wrote:

> 
> Hi
> 
> Does anyone know the correct way to pass the 'device' argument to
> setsockopt with the SO_BINDTODEVICE flag?
> 
> I have tried various methods:
> 
> setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,"eth0")
> setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,inet_aton("eth0"))
> setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("s","eth0"))
> setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("p","eth0"))
> 
> None of these work. I just get a "socket.error: (19, 'No such device')".
> 
> I do have an "eth0" device :-)
> 
> Many thanks
> 
> Richard

Solved it!

So for others that find this in the news archive...

The answer is:

s.setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("%ds" %
(len("eth0")+1,), "eth0"))

Richard





More information about the Python-list mailing list