[Tutor] Loop not executing.

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue Apr 30 21:24:00 CEST 2013


Ajin Abraham wrote:
> Hi,
>    I am using a python snippet that will send some data after the data
> length reaches a particular length. So it is supposed to loop and
> periodically sends the data to an email. i am using smtplib to send
> mail. i will attach the code snippet.
> 
> def email():
>     global data
>     if len(data)>50:
>         ts = datetime.datetime.now()
>         SERVER = "smtp.gmail.com" #Specify Server Here
>         PORT = "587" #Specify Port Here
>         USER="ajin at gmail.com"#Specify Username Here
>         PASS="mypassword"#Specify Password Here
>         FROM = USER#From address is taken from username
>         TO = ["ajin at gmail.com"] #Specify to address.Use comma if more
> than one to address is needed.
>         SUBJECT = " Data at: "+str(ts)
>         MESSAGE = data
>         # formating final email body
>         message = """\
> From: %s
> To: %s
> Subject: %s
> 
> %s
> """ % (FROM, ", ".join(TO), SUBJECT, MESSAGE)
>         server = smtplib.SMTP()
>         server.connect(SERVER,PORT)
>         server.starttls()
>         server.login(USER,PASS)
>         server.sendmail(FROM, TO, message)
>         server.quit()
>         print 'Log Send!!'
>         data=''
>     return True
> 
> This function email() is called periodically and is in a loop. the
> program just execute once and keep running in the loop without exiting
> but the data is mailed only once. i will also attache the Traceback
> details:
> Traceback (most recent call last):
>   File "c:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in Keybo
> ardSwitch
>     return func(event)
>   File "key2.py", line 132, in keypressed
>     email()
>   File "key2.py", line 83, in email
>     server = smtplib.SMTP()
>   File "c:\Python27\lib\smtplib.py", line 258, in __init__
>     fqdn = socket.getfqdn()
>   File "c:\Python27\lib\socket.py", line 142, in getfqdn
>     for name in aliases:
> TypeError: an integer is required
> Log Send!!
> 

Have you looked at the documentation?

Help on method __init__ in module smtplib:
__init__(self, host='', port=0, local_hostname=None, timeout=<object object>) unbound smtplib.SMTP method
    Initialize a new instance.
    
    If specified, `host' is the name of the remote host to which to
    connect.  If specified, `port' specifies the port to which to connect.
    By default, smtplib.SMTP_PORT is used.  An SMTPConnectError is raised
    if the specified `host' doesn't respond correctly.  If specified,
    `local_hostname` is used as the FQDN of the local host.  By default,
    the local hostname is found using socket.getfqdn().



Well that explains the usage of socket.getfqdn() from your trace.


Help on function getfqdn in module socket:
getfqdn(name='')
    Get fully qualified domain name from name.
    
    An empty argument is interpreted as meaning the local host.
    
    First the hostname returned by gethostbyaddr() is checked, then
    possibly existing aliases. In case no FQDN is available, hostname
    from gethostname() is returned.


Hmm, not illuminating anything for me, but you can likely skip all 
this by passing the FQDN (fully qualified domain name) directly to 
smtplib.SMTP( local_hostname='blah.blah.blah').


> 
> I believe someone can help on this matter.
> 
> Regards,
> Ajin Abraham
> 
> Information Security Enthusiast.
> www.ajinabraham.com | www.defconkerala.org
> www.keralacyberforce.in | +91-9633325997
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list