Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33663 > unrolled thread

Choosing Source Address to Bind Socket to in IMAP Client

Started bybrintoul@controlledthinking.com
First post2012-11-20 13:14 -0800
Last post2012-11-20 14:12 -0800
Articles 12 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Choosing Source Address to Bind Socket to in IMAP Client brintoul@controlledthinking.com - 2012-11-20 13:14 -0800
    Re: Choosing Source Address to Bind Socket to in IMAP Client Chris Angelico <rosuav@gmail.com> - 2012-11-21 08:47 +1100
      Re: Choosing Source Address to Bind Socket to in IMAP Client brintoul@controlledthinking.com - 2012-11-20 14:00 -0800
        Re: Choosing Source Address to Bind Socket to in IMAP Client Chris Angelico <rosuav@gmail.com> - 2012-11-21 09:35 +1100
      Re: Choosing Source Address to Bind Socket to in IMAP Client brintoul@controlledthinking.com - 2012-11-20 14:00 -0800
    RE: Choosing Source Address to Bind Socket to in IMAP Client "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-20 21:58 +0000
      Re: Choosing Source Address to Bind Socket to in IMAP Client brintoul@controlledthinking.com - 2012-11-20 14:12 -0800
        RE: Choosing Source Address to Bind Socket to in IMAP Client "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-20 22:41 +0000
          Re: Choosing Source Address to Bind Socket to in IMAP Client brintoul@controlledthinking.com - 2012-11-20 14:55 -0800
            RE: Choosing Source Address to Bind Socket to in IMAP Client "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-20 23:10 +0000
          Re: Choosing Source Address to Bind Socket to in IMAP Client brintoul@controlledthinking.com - 2012-11-20 14:55 -0800
      Re: Choosing Source Address to Bind Socket to in IMAP Client brintoul@controlledthinking.com - 2012-11-20 14:12 -0800

#33663 — Choosing Source Address to Bind Socket to in IMAP Client

Frombrintoul@controlledthinking.com
Date2012-11-20 13:14 -0800
SubjectChoosing Source Address to Bind Socket to in IMAP Client
Message-ID<72731eb3-1521-46f0-8601-8a2df58849d8@googlegroups.com>
Hello:

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?

Thanks for any help...

[toc] | [next] | [standalone]


#33667

FromChris Angelico <rosuav@gmail.com>
Date2012-11-21 08:47 +1100
Message-ID<mailman.102.1353448083.29569.python-list@python.org>
In reply to#33663
On Wed, Nov 21, 2012 at 8:14 AM,  <brintoul@controlledthinking.com> wrote:
> Hello:
>
> 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.)

ChrisA

[toc] | [prev] | [next] | [standalone]


#33670

Frombrintoul@controlledthinking.com
Date2012-11-20 14:00 -0800
Message-ID<2d0c91ed-b3b0-4c1d-bb10-5ec1357ef9ce@googlegroups.com>
In reply to#33667
On Tuesday, November 20, 2012 1:48:46 PM UTC-8, Chris Angelico wrote:
> On Wed, Nov 21, 2012 at 8:14 AM,  <brintoul at controlledthinking.com> wrote:
> 
> > Hello:
> 
> >
> 
> > 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.)
> 
> 
> 
> ChrisA

Unless I'm reading that wrong, that's specifying the address/host to connect to (the destination address) not the source address...

[toc] | [prev] | [next] | [standalone]


#33677

FromChris Angelico <rosuav@gmail.com>
Date2012-11-21 09:35 +1100
Message-ID<mailman.111.1353450918.29569.python-list@python.org>
In reply to#33670
On Wed, Nov 21, 2012 at 9:00 AM,  <brintoul@controlledthinking.com> wrote:
> On Tuesday, November 20, 2012 1:48:46 PM UTC-8, Chris Angelico wrote:
>> On Wed, Nov 21, 2012 at 8:14 AM,  <brintoul at controlledthinking.com> 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

[toc] | [prev] | [next] | [standalone]


#33671

Frombrintoul@controlledthinking.com
Date2012-11-20 14:00 -0800
Message-ID<mailman.106.1353448846.29569.python-list@python.org>
In reply to#33667
On Tuesday, November 20, 2012 1:48:46 PM UTC-8, Chris Angelico wrote:
> On Wed, Nov 21, 2012 at 8:14 AM,  <brintoul at controlledthinking.com> wrote:
> 
> > Hello:
> 
> >
> 
> > 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.)
> 
> 
> 
> ChrisA

Unless I'm reading that wrong, that's specifying the address/host to connect to (the destination address) not the source address...

[toc] | [prev] | [next] | [standalone]


#33669

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-11-20 21:58 +0000
Message-ID<mailman.105.1353448752.29569.python-list@python.org>
In reply to#33663
brintoul@controlledthinking.com wrote:
> 
> Hello:
> 
> 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?

One assumes by programming.

> 
> Thanks for any help...

You are quite welcome. :)

On a less flippant note, maybe some links would help you 
get started.

http://www.doughellmann.com/PyMOTW/imaplib/
http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/ 
http://docs.python.org/2/library/imaplib.html


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


#33673

Frombrintoul@controlledthinking.com
Date2012-11-20 14:12 -0800
Message-ID<ff63b325-4295-43c9-aa07-488a581ce5aa@googlegroups.com>
In reply to#33669
On Tuesday, November 20, 2012 1:59:34 PM UTC-8, Prasad, Ramit wrote:
> brintoul at controlledthinking.com wrote:
> 
> > 
> 
> > Hello:
> 
> > 
> 
> > 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?
> 
> 
> 
> One assumes by programming.
> 
> 
> 
> > 
> 
> > Thanks for any help...
> 
> 
> 
> You are quite welcome. :)
> 
> 
> 
> On a less flippant note, maybe some links would help you 
> 
> get started.
> 
> 
> 
> http://www.doughellmann.com/PyMOTW/imaplib/
> 
> http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/ 
> 
> http://docs.python.org/2/library/imaplib.html
> 
> 
> 
> 
> 
> ~Ramit
> 
> 
> 
> 
> 
> This email is confidential and subject to important disclaimers and
> 
> conditions including on offers for the purchase or sale of
> 
> securities, accuracy and completeness of information, viruses,
> 
> confidentiality, legal privilege, and legal entity disclaimers,
> 
> available at http://www.jpmorgan.com/pages/disclosures/email.

While I appreciate your attempt at humor, this is not information which helps me.  Perhaps you are not familiar with the basics of socket programming. This is all fine and dandy, and it is good to "know what you don't know".  If you're interested, look at the third argument to "create_connection": http://docs.python.org/2/library/socket.html

[toc] | [prev] | [next] | [standalone]


#33678

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-11-20 22:41 +0000
Message-ID<mailman.112.1353451280.29569.python-list@python.org>
In reply to#33673
brintoul@controlledthinking.com 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?
[snip]

> While I appreciate your attempt at humor, this is not information which helps me.  Perhaps you are not familiar
> with the basics of socket programming. This is all fine and dandy, and it is good to "know what you don't know".
> If you're interested, look at the third argument to "create_connection":
> http://docs.python.org/2/library/socket.html

Apologies, I misread your question. 

According to the imaplib docs, you can subclass IMAP4 and override 
`IMAP4.open` to create the socket and bind it to the desired interface.
You could also connect using IMAP4 and then close/reopen the socket
inside the IMAP4 connection object. 


I hope that is more help,
Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


#33680

Frombrintoul@controlledthinking.com
Date2012-11-20 14:55 -0800
Message-ID<ad71a3b0-72e2-43dc-bf3e-30b564f8ad79@googlegroups.com>
In reply to#33678
On Tuesday, November 20, 2012 2:41:58 PM UTC-8, Prasad, Ramit wrote:
> brintoul at controlledthinking.com wrote:
> 
> Apologies, I misread your question. 
> 
> 
> 
> According to the imaplib docs, you can subclass IMAP4 and override 
> 
> `IMAP4.open` to create the socket and bind it to the desired interface.
> 
> You could also connect using IMAP4 and then close/reopen the socket
> 
> inside the IMAP4 connection object. 
> 
> I hope that is more help,
> 
> Ramit
> 

Thanks, yes, that helps!  Just out of curiosity, can you give me a quick link to the docs you are looking at?  I'm not seeing anything which gets very specific in the docs for Python 2.7 at docs.python.org ...

[toc] | [prev] | [next] | [standalone]


#33683

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-11-20 23:10 +0000
Message-ID<mailman.116.1353453408.29569.python-list@python.org>
In reply to#33680
brintoul@controlledthinking.com wrote:
> 
> On Tuesday, November 20, 2012 2:41:58 PM UTC-8, Prasad, Ramit wrote:
> > brintoul at controlledthinking.com wrote:
> >
> > Apologies, I misread your question.
> >
> > According to the imaplib docs, you can subclass IMAP4 and override
> > `IMAP4.open` to create the socket and bind it to the desired interface.
> > You could also connect using IMAP4 and then close/reopen the socket
> > inside the IMAP4 connection object.
> >
> > I hope that is more help,
> > Ramit
> >
> 
> Thanks, yes, that helps!  Just out of curiosity, can you give me a quick link to the docs you are looking at?
> I'm not seeing anything which gets very specific in the docs for Python 2.7 at docs.python.org ...


It is not very specific or helpful but here is the text in question.

'''
IMAP4.open(host, port)
    Opens socket to port at host. This method is implicitly called by the IMAP4 constructor. The connection objects established by this method will be used in the read, readline, send, and shutdown methods. You may override this method.
'''
http://docs.python.org/2.7/library/imaplib.html#imaplib.IMAP4.open


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


#33681

Frombrintoul@controlledthinking.com
Date2012-11-20 14:55 -0800
Message-ID<mailman.114.1353452147.29569.python-list@python.org>
In reply to#33678
On Tuesday, November 20, 2012 2:41:58 PM UTC-8, Prasad, Ramit wrote:
> brintoul at controlledthinking.com wrote:
> 
> Apologies, I misread your question. 
> 
> 
> 
> According to the imaplib docs, you can subclass IMAP4 and override 
> 
> `IMAP4.open` to create the socket and bind it to the desired interface.
> 
> You could also connect using IMAP4 and then close/reopen the socket
> 
> inside the IMAP4 connection object. 
> 
> I hope that is more help,
> 
> Ramit
> 

Thanks, yes, that helps!  Just out of curiosity, can you give me a quick link to the docs you are looking at?  I'm not seeing anything which gets very specific in the docs for Python 2.7 at docs.python.org ...

[toc] | [prev] | [next] | [standalone]


#33674

Frombrintoul@controlledthinking.com
Date2012-11-20 14:12 -0800
Message-ID<mailman.108.1353449554.29569.python-list@python.org>
In reply to#33669
On Tuesday, November 20, 2012 1:59:34 PM UTC-8, Prasad, Ramit wrote:
> brintoul at controlledthinking.com wrote:
> 
> > 
> 
> > Hello:
> 
> > 
> 
> > 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?
> 
> 
> 
> One assumes by programming.
> 
> 
> 
> > 
> 
> > Thanks for any help...
> 
> 
> 
> You are quite welcome. :)
> 
> 
> 
> On a less flippant note, maybe some links would help you 
> 
> get started.
> 
> 
> 
> http://www.doughellmann.com/PyMOTW/imaplib/
> 
> http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/ 
> 
> http://docs.python.org/2/library/imaplib.html
> 
> 
> 
> 
> 
> ~Ramit
> 
> 
> 
> 
> 
> This email is confidential and subject to important disclaimers and
> 
> conditions including on offers for the purchase or sale of
> 
> securities, accuracy and completeness of information, viruses,
> 
> confidentiality, legal privilege, and legal entity disclaimers,
> 
> available at http://www.jpmorgan.com/pages/disclosures/email.

While I appreciate your attempt at humor, this is not information which helps me.  Perhaps you are not familiar with the basics of socket programming. This is all fine and dandy, and it is good to "know what you don't know".  If you're interested, look at the third argument to "create_connection": http://docs.python.org/2/library/socket.html

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web