smtplib send mail dns resolve problem

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Oct 19 17:05:18 CEST 2009


En Mon, 19 Oct 2009 05:44:14 -0200, 星星 <starstarstarpku at gmail.com>  
escribió:

>     my email smtp server hostname can be parsed to 5 ips, for example:
>     ******************************************************************
>      my email smtp server hostname:  email-my.local.com
>      ips through dns parse:
>     1.1.1.1
>     1.1.1.12
>     1.1.13.1
>     1.1.1.14
>     1.1.1.15
>     ******************************************************************
>
>     but when i send mail using smtplib, i was always using one ip, no
> dns re-resolve happened, (for example: only use 1.1.1.12). I checked
> smtplib src code, but i can't find anything strange;

smtplib does not issue a dns query for a MX record, if that is what you  
were expecting. In fact, the Python standard library does not contain any  
module for DNS handling.

>     for i in range(1000):
>         server = smtplib.SMTP(host)

Here, `host` is used directly -- whatever address gethostbyname returns.  
If you want load balancing among the 5 addresses above, you'll have to do  
it yourself:
     host = random.choice(list_of_addresses)

If you don't want to hardcode the addresses, there are a few libraries  
that perform DNS queries in PyPI.

-- 
Gabriel Genellina




More information about the Python-list mailing list