smtplib send mail dns resolve problem

Gabriel Genellina gagsl-py2 at
Mon Oct 19 17:05:18 CEST 2009

En Mon, 19 Oct 2009 05:44:14 -0200, 星星 <starstarstarpku at>  

>     my email smtp server hostname can be parsed to 5 ips, for example:
>     ******************************************************************
>      my email smtp server hostname:
>      ips through dns parse:
>     ******************************************************************
>     but when i send mail using smtplib, i was always using one ip, no
> dns re-resolve happened, (for example: only use 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