473,655 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 <st****@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.

Jul 25 '06 #1
3 8206
[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

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.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 <st****@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.

Jul 25 '06 #3
[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

Jul 25 '06 #4

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

Similar topics

9
10953
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 working piece of code that emails jpg attachments but does not work with any other types of attachments. Could someone tell me how to modify this code to send other types of attachments like the one's stated above(especially PDF's)? Also how do I...
1
2904
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 expertise. I am trying to run Gypsymail.py as a CGI program from a web form. It is nearly all working, except for the part that I have reduced to the following code: #!/usr/bin/python # the line above must contain the path to Python on your...
6
2314
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, were you able to work around it? Here's the traceback (below). Interestingly, if I change ports to the POP port 110, I get a different error, but one that lets me know that I can reach the server. trying to connect on the SMTP port 25: >>>...
0
3038
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 of chars from SSLFakeFile used during TLS instead of timing out the client connection. I am working from my own hacked version of smtplib, but can demonstrate the problem using the standard module (Python 2.3.4). Can anyone suggest a...
6
10010
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 an unexpected error. Here's my sample code: import smtplib
3
4385
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: >>> import smtplib >>> server = smtplib.SMTP('localhost') >>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org', """To: jcaesar@example.org
0
1864
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 login or do the sendmail): I have not had this problem with other mail servers (but only tried two). A solution for gmail seems to be to replace the server.quit() with server.close(). The difference between the two commands is quit() sends a...
5
4681
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: Traceback (most recent call last):
1
2385
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 the from, to, subject, and message text. So I wrote it up as a function and it sort of works, but I get a weird error. When it runs it inserts a "\t" tab character before each item during the send portion (which I can see when I turn on...
0
8380
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.