Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'socket': 0.05; '21,': 0.07; 'override': 0.07; 'parameter': 0.07; 'referring': 0.07; 'python': 0.09; 'specifying': 0.09; 'subclass': 0.09; 'subject:Source': 0.09; 'underlying': 0.09; 'wrong,': 0.09; 'def': 0.10; 'suggest': 0.11; '(the': 0.15; '(note': 0.16; 'ah,': 0.16; 'bind': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function?': 0.16; 'imap4': 0.16; 'parameter,': 0.16; 'self.port))': 0.16; 'socket.': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'specify': 0.17; 'creates': 0.18; 'skip:_ 20': 0.22; 'machine': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'creating': 0.26; 'am,': 0.27; 'separate': 0.27; 'message- id:@mail.gmail.com': 0.27; 'interface': 0.27; 'this?': 0.28; 'chris': 0.28; 'run': 0.28; 'address)': 0.29; 'bad.': 0.29; 'source': 0.29; 'class': 0.29; "i'm": 0.29; 'url:python': 0.32; 'could': 0.32; 'getting': 0.33; 'skip:s 30': 0.33; 'docs': 0.33; 'function.': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'server': 0.35; 'nov': 0.35; 'received:209.85': 0.35; 'url:org': 0.36; 'url:library': 0.36; 'client': 0.36; 'address.': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'url:docs': 0.38; 'shows': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'address': 0.60; 'here:': 0.62; '20,': 0.65; 'subject:Client': 0.84; '9:00': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=USrR5EO8XRWGAjjsHS8INyzINfpWqiUfCUVSdFlv2I0=; b=Us3eL7bWqCRN/+hY3hoq9CQiqljbsAOqXpKeGRIVDMmv/505A0pvNL3a9ciJiHkf2m qrzCU9yKqxuFk+Dc6lRSDgzmecwZsWvaGOO5TYTKaIp9TrhtUHP74AJ5mLzzi0hb9nm6 atFjo9eV+jXkC4yheK2cFDAoATAdl3u/RnbXHanwSogxJkv7jS5Bw+lM4evo1aqkehdU /thK75FhomjfPfC2HYrHrMFrU23meb1CrEbbKdvY8OkC2rzWn8t+5sgJHO6Njv69fnmD kdz/4rVZfnEILA9+p5TgbaboZFyMVLnFffi8MCK4Xs4yUYTAIN7Jt47eIqNfTE2wkmxR VJSg== MIME-Version: 1.0 In-Reply-To: <2d0c91ed-b3b0-4c1d-bb10-5ec1357ef9ce@googlegroups.com> References: <72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com> <2d0c91ed-b3b0-4c1d-bb10-5ec1357ef9ce@googlegroups.com> Date: Wed, 21 Nov 2012 09:35:14 +1100 Subject: Re: Choosing Source Address to Bind Socket to in IMAP Client From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353450918 news.xs4all.nl 6915 [2001:888:2000:d::a6]:35313 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33677 On Wed, Nov 21, 2012 at 9:00 AM, wrote: > On Tuesday, November 20, 2012 1:48:46 PM UTC-8, Chris Angelico wrote: >> On Wed, Nov 21, 2012 at 8:14 AM, wrote: >> >> > I have a multihomed machine that I would like to run the Python imaplib's IMAP4 client on. I would like to be able to specify which interface the underlying socket will bind to as its source address. How could I best do this? >> >> You're referring to this function? >> >> http://docs.python.org/3.3/library/imaplib.html#imaplib.IMAP4 >> >> The docs suggest that you can simply pass it a parameter to specify >> the address to bind to. (Note that you bind to addresses, not >> interfaces. Figuring out which interface has which address is a >> separate issue.) >> > > Unless I'm reading that wrong, that's specifying the address/host to connect to (the destination address) not the source address... Ah, whoops! My bad. For some reason I was thinking that was creating a server socket. Sorry! Poking around in the source (imaplib.py) shows that it creates a socket here: class IMAP4: def _create_socket(self): return socket.create_connection((self.host, self.port)) Adding a third parameter to create_connection would do what you want. (Note that this is currently getting one parameter, not two.) http://docs.python.org/3.3/library/socket.html#socket.create_connection My recommendation: Subclass IMAP4 and override this one function. ChrisA