English 中文(简体)
如何使用smtplib和Python保持SMTP连接打开?
原标题:How can I hold a SMTP connection open with smtplib and Python?

我需要检查SMTP服务器的超时,但我的套接字刚刚关闭。我做错了什么?这是我的测试:

#!/usr/bin/python
import smtplib
import time
import datetime
import socket
socket.setdefaulttimeout(1800)


now = time.time()
server = smtplib.SMTP()
server.set_debuglevel(1)
server.connect( mx.foo.bar , 25 )
(code,resp) = server.docmd( NOOP )
then = time.time()

print then-now

让我们希望这能奏效。

问题回答

嗯,我还没有找到任何方法来保持smtplib打开的smtp连接。

But, if you want to reuse a connection without closing (yes, opening a connection takes time, 2-3 secs), you can test the connection first. To do this, issue a NOOP command and test for status == 250. If not, then you can open a connection and send out your mail. And you can choose to not quit() the connection until you are done.

import smtplib

def create_conn():
    conn = smtplib.SMTP( smtp.gmail.com , 587)
    ...
    return conn

def is_connected(conn):
    try:
        status = conn.noop()[0]
    except:  # smtplib.SMTPServerDisconnected
        status = -1
    return True if status == 250 else False

你确定要断开连接吗?当我在postfix服务器上运行上述代码时,我得到:

connect: ( server ,  25 )
connect: ( ip.address , 25)
reply:  220 server ESMTP Postfix
 
reply: retcode (220); Msg: nserver ESMTP Postfix
connect: server ESMTP Postfix
send:  NOOP
 
reply:  250 2.0.0 Ok
 
reply: retcode (250); Msg: 2.0.0 Ok
0.0531799793243

docmd不阻止,服务器响应,程序退出。因此,当程序退出时,我会断开连接。

如果我打开一个python命令行并执行以下操作:

>> import smtplib
>> server = smtplib.SMTP()
>> server.connect( server )
>> server.docmd( NOOP )
(250,  2.0.0 Ok )
>> ## let it sit for 5 minutes
>> server.docmd( NOOP )
(421,  4.4.2 server Error: timeout exceeded )

我的日志证实了这一点:

Oct 20 10:45:35 [postfix/smtpd] connect from unknown[ip.address]
Oct 20 10:50:10 [postfix/smtpd] timeout after NOOP from unknown[ip.address]

检查您的邮件服务器设置-他们可能正在切断连接。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签