smtplib send mail dns resolve problem

Discussion in 'Python' started by 星星, Oct 19, 2009.

  1. 星星

    星星 Guest

    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!
     
    星星, Oct 19, 2009
    #1
    1. Advertising

  2. En Mon, 19 Oct 2009 05:44:14 -0200, 星星 <>
    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
     
    Gabriel Genellina, Oct 19, 2009
    #2
    1. Advertising

  3. 星星

    星星 Guest

    On 10月19æ—¥, 下åˆ11æ—¶05分, "Gabriel Genellina" <>
    wrote:
    > En Mon, 19 Oct 2009 05:44:14 -0200, 星星 <>  
    > 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


    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).
     
    星星, Oct 20, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Kansky

    DNS Resolve IP to HostName

    Mike Kansky, Jun 6, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    5,032
    Mike Kansky
    Jun 6, 2006
  2. Replies:
    1
    Views:
    10,168
  3. Nicopilami
    Replies:
    1
    Views:
    2,564
    bruce barker
    Apr 2, 2008
  4. Evan
    Replies:
    3
    Views:
    473
  5. Sam Roberts
    Replies:
    0
    Views:
    213
    Sam Roberts
    Mar 20, 2005
Loading...

Share This Page