smtplib send mail dns resolve problem

Æ

星星

hello everyone,
I am using smtplib to send email, and i meet such a problem:

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;

here is my smtplib send mail client code:

********************************************************************
for i in range(1000):
server = smtplib.SMTP(host)
try:
server.sendmail(fromaddr, toaddr, msg)
except Exception, e:
logger.warning('sendmail exception')
else:
pass
finally:
server.quit()
time.sleep(0.3)

********************************************************************

Can anyone give some tips? Thank you!
 
G

Gabriel Genellina

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.
 
Æ

星星

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.


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.

thanks very much! I tried your suggestion and it worked!

But I still wonder why gethostbyname returns the same address all the
time(maybe 50000 times).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top