Choosing Source Address to Bind Socket to in IMAP Client

B

brintoul

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...
 
C

Chris Angelico

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
 
P

Prasad, Ramit

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.
 
B

brintoul

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...
 
B

brintoul

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...
 
B

brintoul

One assumes by programming.







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
 
B

brintoul

One assumes by programming.







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
 
C

Chris Angelico

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
 
P

Prasad, Ramit

[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.
 
B

brintoul

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 ...
 
P

Prasad, 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top