Choosing Source Address to Bind Socket to in IMAP Client

Discussion in 'Python' started by brintoul@controlledthinking.com, Nov 20, 2012.

  1. Guest

    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...
    , Nov 20, 2012
    #1
    1. Advertising

  2. On Wed, Nov 21, 2012 at 8:14 AM, <> 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
    Chris Angelico, Nov 20, 2012
    #2
    1. Advertising

  3. 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.
    Prasad, Ramit, Nov 20, 2012
    #3
  4. Guest

    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...
    , Nov 20, 2012
    #4
  5. Guest

    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...
    , Nov 20, 2012
    #5
  6. Guest

    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
    , Nov 20, 2012
    #6
  7. Guest

    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
    , Nov 20, 2012
    #7
  8. 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, <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
    Chris Angelico, Nov 20, 2012
    #8
  9. 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 andoverride
    `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 includingon 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.
    Prasad, Ramit, Nov 20, 2012
    #9
  10. Guest

    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 ...
    , Nov 20, 2012
    #10
  11. 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 itto 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 disclaimersand
    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.
    Prasad, Ramit, Nov 20, 2012
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Paul Calderone
    Replies:
    0
    Views:
    965
    Jean-Paul Calderone
    Jan 27, 2009
  2. Henrik Ormåsen

    Curier-IMAP and imap.create()

    Henrik Ormåsen, Aug 19, 2006, in forum: Ruby
    Replies:
    0
    Views:
    175
    Henrik Ormåsen
    Aug 19, 2006
  3. Jon Fi
    Replies:
    4
    Views:
    522
    Kashia Buch
    Oct 21, 2006
  4. jasonnaylor
    Replies:
    1
    Views:
    250
    jasonnaylor
    Apr 16, 2008
  5. Adam Akhtar
    Replies:
    1
    Views:
    174
    Eric Hodel
    Dec 15, 2008
Loading...

Share This Page