smtplib timeout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart D. Gathman

    #1

    smtplib timeout

    I am doing SMTP callbacks in a Python milter
    (http://pymilter.sourceforge.net) using the smtplib module. For some
    spammer MXes, it takes days (!) before smtplib.sendmai l will return.
    Since the spammer connects to us every few seconds, this quickly leads to
    a problem :-)

    I need to set a timelimit for the operation of
    smtplib.sendmai l. It has to be thread based, because pymilter uses
    libmilter which is thread based. There are some cookbook recipies which
    run a function in a new thread and call Thread.join(tim eout). This
    doesn't help, because although the calling thread gets a nice timeout
    exception, the thread running the function continues to run. In fact, the
    problem is worse, because even more threads are created.

    --
    Stuart D. Gathman <stuart@bmsi.co m>
    Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
    "Confutatis maledictis, flamis acribus addictis" - background song for
    a Microsoft sponsored "Where do you want to go from here?" commercial.

  • Alan Kennedy

    #2
    Re: smtplib timeout

    [Stuart D. Gathman]
    I need to set a timelimit for the operation of
    smtplib.sendmai l. It has to be thread based, because pymilter uses
    libmilter which is thread based. There are some cookbook recipies which
    run a function in a new thread and call Thread.join(tim eout). This
    doesn't help, because although the calling thread gets a nice timeout
    exception, the thread running the function continues to run. In fact, the
    problem is worse, because even more threads are created.
    Have you tried setting a default socket timeout, which applies to all
    socket operations?

    Here is a code snippet which times out for server connections. Timeouts
    should also work for sending and receiving on sockets that are already
    open, i.e. should work for the smtplib.sendmai l call.

    =============== ===============
    import socket
    import smtplib

    dud_server = '192.168.1.1'
    timeout_value = 1.0 # seconds

    socket.setdefau lttimeout(timeo ut_value)

    print "connecting to server: %s" % dud_server
    try:
    connection = smtplib.SMTP(du d_server)
    except socket.timeout:
    print "server timed out"
    =============== ===============

    HTH,

    --
    alan kennedy
    ------------------------------------------------------
    email alan: http://xhaus.com/contact/alan

    Comment

    • Stuart D. Gathman

      #3
      Re: smtplib timeout

      On Tue, 25 Jul 2006 09:21:40 -0700, Alan Kennedy wrote:
      [Stuart D. Gathman]
      >I need to set a timelimit for the operation of
      >smtplib.sendma il. It has to be thread based, because pymilter uses
      >libmilter which is thread based.
      >
      Have you tried setting a default socket timeout, which applies to all
      socket operations?
      Does this apply to all threads, is it inherited when creating threads, or
      does each thread need to specify it separately?

      --
      Stuart D. Gathman <stuart@bmsi.co m>
      Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
      "Confutatis maledictis, flamis acribus addictis" - background song for
      a Microsoft sponsored "Where do you want to go from here?" commercial.

      Comment

      • Alan Kennedy

        #4
        Re: smtplib timeout

        [Stuart D. Gathman]
        >>I need to set a timelimit for the operation of
        >>smtplib.sendm ail. It has to be thread based, because pymilter uses
        >>libmilter which is thread based.
        [Alan Kennedy]
        >Have you tried setting a default socket timeout, which applies to all
        >socket operations?
        [Stuart D. Gathman]
        Does this apply to all threads, is it inherited when creating threads, or
        does each thread need to specify it separately?
        It is a module level default, which affects all socket operations after
        the socket.setdefau lttimeout() call, regardless of whether they are in
        threads or not.

        So you only need to call it once, probably before any other processing
        takes place.

        =============== =============== =============== ====
        import socket
        import smtplib
        import threading

        dud_server = '192.168.1.1'
        timeout_value = 1.0 # seconds

        socket.setdefau lttimeout(timeo ut_value)

        def do_connect(tno) :
        print "Thread%d: connecting to server: %s" % (tno, dud_server)
        try:
        connection = smtplib.SMTP(du d_server)
        except socket.timeout:
        print "Thread%d: server timed out" % tno

        for x in range(5):
        t = threading.Threa d(target=do_con nect, args=(x,))
        t.start()
        =============== =============== =============== ====

        C:\>python smtp_timeout.py

        Thread0: connecting to server: 192.168.1.1
        Thread1: connecting to server: 192.168.1.1
        Thread2: connecting to server: 192.168.1.1
        Thread3: connecting to server: 192.168.1.1
        Thread4: connecting to server: 192.168.1.1
        Thread0: server timed out
        Thread1: server timed out
        Thread2: server timed out
        Thread4: server timed out
        Thread3: server timed out

        --
        alan kennedy
        ------------------------------------------------------
        email alan: http://xhaus.com/contact/alan

        Comment

        Working...