Skip to content
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 SMTP_SSL not working. #48720

Closed
lmctv mannequin opened this issue Nov 30, 2008 · 21 comments
Closed

smtplib SMTP_SSL not working. #48720

lmctv mannequin opened this issue Nov 30, 2008 · 21 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@lmctv
Copy link
Mannequin

lmctv mannequin commented Nov 30, 2008

BPO 4470
Nosy @pitrou, @giampaolo, @tiran, @lmctv, @bitdancer, @csabella
Dependencies
  • bpo-4066: smtplib SMTP_SSL._get_socket doesn't return a value
  • Files
  • smtplib_01_default_port.diff
  • smtplib_02_fix_ssl.diff
  • smtplib_03_use_makefile.diff
  • smtplib_04_remove_fakefile.diff
  • smtplib_05_shutdown_socket.diff
  • test_smtpnet.py
  • smtplib_05_shutdown_socket_v2.patch: Updated smtplib_05_shutdown_socket.diff
  • v2_01_fix_lmtp_init: Use class attribute for default connection port
  • 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:

    assignee = None
    closed_at = <Date 2018-05-03.11:49:11.967>
    created_at = <Date 2008-11-30.16:23:33.036>
    labels = ['type-bug', 'library']
    title = 'smtplib SMTP_SSL not working.'
    updated_at = <Date 2018-05-03.11:49:11.949>
    user = 'https://github.com/lmctv'

    bugs.python.org fields:

    activity = <Date 2018-05-03.11:49:11.949>
    actor = 'cheryl.sabella'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-05-03.11:49:11.967>
    closer = 'cheryl.sabella'
    components = ['Library (Lib)']
    creation = <Date 2008-11-30.16:23:33.036>
    creator = 'lcatucci'
    dependencies = ['4066']
    files = ['12197', '12198', '12199', '12200', '12201', '12964', '22398', '22401']
    hgrepos = []
    issue_num = 4470
    keywords = ['patch']
    message_count = 21.0
    messages = ['76640', '76759', '88235', '138235', '138519', '138520', '138521', '138523', '138552', '138555', '138561', '138581', '138688', '189591', '189593', '189594', '189597', '189617', '189619', '275022', '316120']
    nosy_count = 8.0
    nosy_names = ['pitrou', 'bronger', 'giampaolo.rodola', 'christian.heimes', 'lcatucci', 'r.david.murray', 'catalin.iacob', 'cheryl.sabella']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4470'
    versions = ['Python 3.4']

    @lmctv
    Copy link
    Mannequin Author

    lmctv mannequin commented Nov 30, 2008

    The enclosed patch does three things:

    1. enables SMTP_SSL working: the _get_socket method was setting
      self.sock instead of returning the socket to the caller, which
      did reset self.sock to None
    2. replace home-grown SSLFakeFile() with calls to ssl.socket's makefile()
      calls both in the starttls and in the SMTP_SSL cases
    3. shutdown sockets before closing them, to avoid server-side piling and
      connection refused on connection-limited servers
      The last change is just a cosmetical refactoring, but it really helps
      the SMTP_SSL case: default_port should really be a class attribute,
      instead of being set at __init__ time.

    @lmctv lmctv mannequin added the stdlib Python modules in the Lib dir label Nov 30, 2008
    @lmctv
    Copy link
    Mannequin Author

    lmctv mannequin commented Dec 2, 2008

    I've reworked the patch into a series, like haypo requested for
    poplib and imaplib.

    @devdanzin devdanzin mannequin added the type-bug An unexpected behavior, bug, or error label May 10, 2009
    @bitdancer
    Copy link
    Member

    With the closure of 4066 all the tests in the test patch pass, so I'm
    lowering the piority of this ticket.

    I haven't reviewed the other patches, but the tests in the test patch
    appear to be doing tests in the setup method, which doesn't seem like a
    good idea.

    @bronger
    Copy link
    Mannequin

    bronger mannequin commented Jun 13, 2011

    I still have to apply Catucci's patch (or a modification of) after every Ubuntu installation or upgrade. Otherwise, I get

    ...
    File "/usr/lib/python2.7/smtplib.py", line 752, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout)
    File "/usr/lib/python2.7/smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
    File "/usr/lib/python2.7/smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
    File "/usr/lib/python2.7/smtplib.py", line 757, in _get_socket
    new_socket = socket.create_connection((host, port), timeout)
    File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
    socket.error: [Errno 111] Connection refused

    But isn't it that bpo-4066 should have solved the critical part this issue pair 4066/4470?

    @bitdancer
    Copy link
    Member

    Torsten, can you provide a clear, failing unittest for this?

    @bronger
    Copy link
    Mannequin

    bronger mannequin commented Jun 17, 2011

    No, I don't know how to do that. All I can provide is a minimal version of my code that triggers the above mentioned traceback. It is:

    import smtplib
    
    s = smtplib.SMTP_SSL("relay-auth.rwth-aachen.de")
    s.login("***", "***")
    s.sendmail("bronger@physik.rwth-aachen.de", "bronger.randys@googlemail.com"], "Hello")

    I hope it helps to understand what I mean.

    @bronger
    Copy link
    Mannequin

    bronger mannequin commented Jun 17, 2011

    Sorry, it must be:

    import smtplib
    
    s = smtplib.SMTP_SSL("relay-auth.rwth-aachen.de")
    s.login("***", "***")
    s.sendmail("bronger@physik.rwth-aachen.de", ["bronger.randys@googlemail.com"], "Hello")

    (A bracket was missing.)

    @bitdancer
    Copy link
    Member

    According to your traceback you should be seeing the error in the first line (the creation of the SMTP_SSL object). If I run that line at the python prompt of python2.7.1, I get your connection failure. If I run it using 2.7 tip (or 3.3 tip), the connection succeeds. I know there have been other fixes in this area, but I don't know which one makes the difference.

    Torsten, can you test with 2.7.2 and/or 2.7 tip?

    I'm not sure what is left to do in this issue. Do you have an opinion, Lorenzo?

    @lmctv
    Copy link
    Mannequin Author

    lmctv mannequin commented Jun 17, 2011

    On Fri, 17 Jun 2011, R. David Murray wrote:

    RDM>
    RDM> R. David Murray <rdmurray@bitdance.com> added the comment:
    RDM>
    RDM> According to your traceback you should be seeing the error in the
    RDM> first line (the creation of the SMTP_SSL object). If I run that line
    RDM> at the python prompt of python2.7.1, I get your connection failure.
    RDM> If I run it using 2.7 tip (or 3.3 tip), the connection succeeds. I
    RDM> know there have been other fixes in this area, but I don't know which
    RDM> one makes the difference.
    RDM>
    RDM> Torsten, can you test with 2.7.2 and/or 2.7 tip?
    RDM>
    RDM> I'm not sure what is left to do in this issue. Do you have an
    RDM> opinion, Lorenzo?
    RDM>

    Torsten, would you mind letting us know both the python and the .deb
    version by running

    $ python2.7 --version

    and

    $ dpkg -l python2.7

    At least in debian's python2.7_2.7.1-8, default_port is still an instance
    attribute instead of a class attribute.

    If the ubuntu situation is the same, the missing patch is
    smtplib_01_default_port.diff , and you could work-around the problem by
    explicitly calling s = smtplib.SMTP_SSL("relay-auth.rwth-aachen.de", 465).

    The patch got in with:

    changeset: 69931:bcf04ced5ef1
    branch: 2.7
    parent: 69915:7c3a20b5943a
    user: Antoine Pitrou <solipsis@pitrou.net>
    date: Sat May 07 19:59:33 2011 +0200
    summary: Issue bpo-11927: SMTP_SSL now uses port 465 by default as
    documented. Patch by Kasun Herath.

    The last hunk, which fixes LMTP is still missing.

    @@ -776,8 +777,9 @@
    authentication, but your mileage might vary."""

         ehlo_msg = "lhlo"
    +    default_port = LMTP_PORT
    -    def __init__(self, host = '', port = LMTP_PORT, local_hostname = 
    None):
    +    def __init__(self, host = '', port = 0, local_hostname = None):
             """Initialize a new instance."""
             SMTP.__init__(self, host, port, local_hostname)

    Have a nice week-end, yours

    lorenzo
    

    +-------------------------+----------------------------------------------+
    | Lorenzo M. Catucci | Centro di Calcolo e Documentazione |
    | catucci@ccd.uniroma2.it | Università degli Studi di Roma "Tor Vergata" |
    | | Via O. Raimondo 18 ** I-00173 ROMA ** ITALY |
    | Tel. +39 06 7259 2255 | Fax. +39 06 7259 2125 |
    +-------------------------+----------------------------------------------+

    @cataliniacob
    Copy link
    Mannequin

    cataliniacob mannequin commented Jun 17, 2011

    Most of the problems in this issue were solved already so it could almost be closed:

    Torsten's problem was addressed by bcf04ced5ef1.

    I'm not sure what is left to do in this issue.

    The only patch remaining is patch 5. I attached an updated version against tip of default branch. My patch mimics shutdown in imaplib.py in that it silences ENOTCONN. However I don't have a test that fails without the shutdown and I don't know if checking ENOTCONN is really needed. I tried to get shutdown to produce ENOTCONN by using Postfix as a server with smtpd_timeout=5s, connecting to it and waiting idle for more than 5 seconds before doing close(). In the Postfix logs I see that Postfix disconnects after 5 seconds of inactivity but doing shutdown afterwards doesn't trigger any exception so the ENOTCONN part remains unexercised.

    My patch also adds shutdown method and SHUT_RDWR constant to mock_socket.py since otherwise test_smtplib fails.

    (Added Antoine to nosy because he reviewed the patches for bpo-11927 and bpo-11893)

    @bronger
    Copy link
    Mannequin

    bronger mannequin commented Jun 17, 2011

    My Python version is "Python 2.7.1+" and the package is called "python2.7 2.7.1-5ubuntu2" (Ubuntu Natty).

    @lmctv
    Copy link
    Mannequin Author

    lmctv mannequin commented Jun 18, 2011

    Just finished testing both 2.7 and default branches' socket close behaviour, and it seems 05 is not strictly needed.

    I'd still prefer if smtplib_05_shutdown_socket_v2.patch since, this way the REMOTE socket close will be unconditionally correct, instead of being dependent on GC artifacts.

    @lmctv
    Copy link
    Mannequin Author

    lmctv mannequin commented Jun 20, 2011

    I'd still prefer if smtplib_05_shutdown_socket_v2.patch could get in,
    ^^^^^^^^^^^^^^
    since, this way the REMOTE socket close will be unconditionally correct,
    instead of being dependent on GC artifacts.

    @bronger
    Copy link
    Mannequin

    bronger mannequin commented May 19, 2013

    For five Ubuntu releases now, I apply this patch. In contrast to Catalin's statement, it is not solved for me unless the upstream changes of two years ago haven't yet found their way into Ubuntu. I'm currently using Python 2.7.4 on Ubuntu 13.04. That said, the patch solves my issue every time.

    @pitrou
    Copy link
    Member

    pitrou commented May 19, 2013

    When you say "I apply this patch", you mean smtplib_05_shutdown_socket_v2.patch, right?

    @pitrou
    Copy link
    Member

    pitrou commented May 19, 2013

    In any case, I'm growing wary of bugfix regressions right now, so I would only apply the patch on the default branch.

    @bronger
    Copy link
    Mannequin

    bronger mannequin commented May 19, 2013

    Sorry, after having had another look at it, I realised that I have a different SSMTP issue now, non-Python-related. So for me, Ubuntu 13.04 indeed solves my old issue.

    @bitdancer
    Copy link
    Member

    Lorenzo, any chance you could supply a unit test that fails without smtplib_05_shutdown_socket.diff applied?

    @lmctv
    Copy link
    Mannequin Author

    lmctv mannequin commented May 19, 2013

    On Sun, 19 May 2013, R. David Murray wrote:

    RDM>
    RDM> R. David Murray added the comment:
    RDM>
    RDM> Lorenzo, any chance you could supply a unit test that fails without
    RDM> smtplib_05_shutdown_socket.diff applied?
    RDM>

    It would really be a functional test, since the problem with half-open
    connection pile-up stems from smtp server's access control rules.

    To test we should setup a fake smtp server, which forbids having multiple
    connections from the same IP address, and connect twice in a row to the
    fake server. I'm not sure I'm able to implement both an smtpd.py server
    serving more than one connection and the client code in a race-free way in
    the same "unit" test. Will try in the next week.

    Thank you very much,

    lorenzo
    

    @tiran
    Copy link
    Member

    tiran commented Sep 8, 2016

    The bug is 8 years old and hasn't seen activity for three years. Is SMTP over SSL still broken for you?

    @csabella
    Copy link
    Contributor

    csabella commented May 3, 2018

    This issue has been in pending status for over 18 months following Christian's question to the OP, so closing as out of date.

    @csabella csabella closed this as completed May 3, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants