473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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.sendmail 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.sendmail. 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(timeout). 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 <st****@bmsi.com>
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.

Jul 25 '06 #1
3 8194
[Stuart D. Gathman]
I need to set a timelimit for the operation of
smtplib.sendmail. 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(timeout). 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.sendmail call.

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

dud_server = '192.168.1.1'
timeout_value = 1.0 # seconds

socket.setdefaulttimeout(timeout_value)

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

HTH,

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

Jul 25 '06 #2
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.sendmail. 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 <st****@bmsi.com>
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.

Jul 25 '06 #3
[Stuart D. Gathman]
>>I need to set a timelimit for the operation of
smtplib.sendmail. 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.setdefaulttimeout() 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.setdefaulttimeout(timeout_value)

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

for x in range(5):
t = threading.Thread(target=do_connect, 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

Jul 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bill | last post by:
I am trying to have the capability to email attachments. Specifically I want to be able to email a specific attachment that I name that may be a PDF document, text doc, etc. I already have a...
1
by: David Hughes | last post by:
I wonder if anyone can help me resolve this problem. Although my ISP (One and One) provides the facility to run Python 2.2 CGI programs, their technical support people don't seem to have any Python...
6
by: stewart.midwinter | last post by:
I'm having problem with a script that used to work under Win2k but is now broken after an install of WinXP Pro. I can no longer connect to a local mail server. Has anyone else seen this? If so,...
0
by: Tim Williams | last post by:
I have a working SMTP client that I need to add TLS capability to, I absolutely need the client to timeout within a specified time, but when I use the sock.timeout() line it freezes the reading...
6
by: Matthias Kluwe | last post by:
Hi! After getting a @gmail.com address, I recognized I had to use TLS in my python scripts using smtplib in order to get mail to the smtp.gmail.com server. Things work well so far, apart from...
3
by: Van_Gogh | last post by:
Hi, I am learning how to use the smtplib module, but am having some very early problems, maybe because I don't understand it. So, am I correct that by following the example in the Python: >>>...
0
by: Roger | last post by:
I am having a problem sending email through smtp.gmail.com using smtplib. Everything works and the mail is sent and received, except quit. The following shows the problem (without bothering to...
5
by: zxo102 | last post by:
Hi, I am trying to use python module smtplib to send my email out on window xp (localhost). import smtplib server = smtplib.SMTP('localhost') but I got the error information as follows: ...
1
by: Hunter | last post by:
I am writing a script that needs to send some emails. And I've used smtplib in the past and it is pretty easy. But I thought, gee it would be easier if I could just call it as a function, passing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.