-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
smtplib: add ability to bind to specific source IP address/port #55490
Comments
In smtplib there is now way to bind to an specific source address on a machine with multiple interfaces; also, there no way to control the source port for the connection. Since 2.7, socket.create_connection accepts a source_address parameter, a (host, port) tuple for the socket to bind to as its source address before connecting. If host or port are '' or 0 respectively the OS default behavior will be used. I would like to add source_ip and source_port parameters to smtplib.SMTP, default to '' and 0 respectively. It is a small change with no impact over existing code. Should I submit a patch? |
This sounds like a reasonable feature request. If you would like to propose a patch against trunk (py3k, what will become 3.3), I will take a look at it. |
+1, this has been done for other modules such as ftplib as well and probably could be done for others such as httplib and poplib. |
It would be better to provide a unique source_address parameter defaulting to None, for consistency with socket.create_connection() expecting a (addr, port) tuple. |
Seems like the signature of Lib.test.mock_socket.create_connection does not match socket.create_connection since 2.7. I need help with the docs; the last argument is positional and optional, using the `[ ]' notation. What is the notation for the new keyword arguments? |
You can add a fake parameter which does nothing.
fun(a, b, c=None, d=None) |
Also, it is my first patch and I have no clue about what I'm doing, I don't expect to get it right in the first try - please point any mistakes. |
@giampaolo: should I change the text in Doc/library/smtplib.rst or it is infered from the docstrings? |
Yes, you must update Doc/library/smtplib.rst and write tests. Updating the docstring is usually optional but I see smtplib module is already well documented so I'd update docstrings as well. As for how to write a patch see: |
My first idea was to make the argument a tuple to match socket.create_connection(), but SMTP uses host and port arguments, for consistency it's better havin separate source_ip and source_port arguments. As a side effect, it makes easier to specify only source_port. The submited patch includes tests and updated docstrings; At the smtplib.rst, for example: SMTP(host='', port=0, local_hostname=None[, timeout]) Would be ok to change it like: SMTP(host='', port=0, local_hostname=None[, timeout], source_ip='', source_port=0) |
We already have 3 places where a tuple is used: socket.socket.bind Changing this notation in smtplib for such a rarely used feature is not worth the effort, imo. PS - I modified smtplib.py in meantime. Please update your local copy. |
Ok, I changed to a single parameter matching socket.create_connection(). I made my best to update tests and docs, but I don't have a clue if it is right. |
Follow my comments:
Make that default to None instead and pass it as-is to socket.create_connection().
There's no need to store the source address as an instance attribute. I think source_address no longer makes sense in UNIX sockets / LMTP class. >>> import socket
>>> sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
>>> sock.bind(("", 0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in bind
TypeError: argument must be string or read-only character buffer, not tuple
>>> |
Ok, what if user initialize with something like smtplib.SMTP(source_address=('200.165.12.1', 25)) and calls connect without source_address? |
You are right, I haven't thought about that. |
Giampaolo, Thanks for your kind review, I will send a patch with suggested changes this weekend. Grazie,Paulo |
Patch attached. |
oops... |
Sorry for the last post, I had the impression that I forgot to attach the patch. I'm slow this Monday. |
Is there anything I should improve in order to get this patch commited? |
New changeset 26839edf3cc1 by Senthil Kumaran in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: