how to get python socket to use a specific interface

Gene Heskett gheskett at wdtv.com
Fri Oct 23 07:37:15 EDT 2015


On Friday 23 October 2015 05:03:41 Robin Becker wrote:

> I need to run lynx on a ubuntu headless server with a specific IP
> address in order to try and cancel an email blacklist.
>
> Lynx doesn't provide a way to do that so I thought to use a local
> proxy. I tried pymiproxy and hacked the connection code so it looked
> like this
>
>
> proxy.py
> ........
>
> > from socket import socket, SOL_SOCKET
> > .......
> > import os
> > BIND_ADDRESS = os.environ.get('PYMIPROXY_BIND_ADDRESS',None)
> > BIND_DEVICE = os.environ.get('PYMIPROXY_BIND_DEVICE',None)
> > .............
> >         # Connect to destination
> >         sock = self._proxy_sock = socket()
> >         sock.settimeout(10)
> >         if BIND_ADDRESS:
> >             sock.bind((BIND_ADDRESS, 0))
> >         if BIND_DEVICE:
> >             sock.setsockopt(SOL_SOCKET, 25, BIND_DEVICE)
> >         sock.connect((self.hostname, int(self.port)))
>
> 25 is derived from /usr/include/asm-generic/socket.h
>
> > #define SO_BINDTODEVICE 25
>
> This works if I export BIND_DEVICE='eth0' and run as root, but my
> desired interface is actually called 'eth0:0' and when I run with that
> exported I get a device error. Does anyone know what I should call the
> device? --
> Robin Becker

Using eth0:0 is normally a method to setup eth0 to respond to a 2nd 
IPV4/IPV6 address.  Have you done the ifconfig steps to enable that?  If 
its been done, you will see it's 2nd address in an ifconfig query.  Man 
pages are wunnerful things.

I think you can use it as a normal user, but the ifconfig IIRC has to be 
done by root, or sudo.

One of my printers has a cat5 socket, which turns out to be its fastest 
interface, and I had to setup an eth0:0 in 192.168.0.## block for a day 
or so until I had wandered thru its menus and found I could put it on my 
local networks class D block and did, so eth0:0 was no longer required.

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>



More information about the Python-list mailing list