[Tutor] Website monitoring program.

Kent Johnson kent37 at tds.net
Mon Nov 21 04:26:10 CET 2005


Adisegna wrote:
> How do I get the counting loop to come back around? It makes one pass 
> fine. How do I get it to come back up and go through again? 

You have to indent the statement 'count += 1' so it is part of the loop. But you misunderstand the for loop - the count variable is not needed at all. Your variable 'i' will receive each element of urls, one each time through the loop. For a simpler example,

 >>> u = ['a', 'b', 'c']
 >>> for letter in u:
 ...   print letter
 ...
a
b
c

So instead of 
count = 0
for i in urls: 
    web = urls[count]

you can write simply
for web in urls:

See the Python tutorial for more examples of for loops:
http://docs.python.org/tut/node6.html#SECTION006200000000000000000

Kent

> 
> Thanks
> 
> -------------------------------------------------------
> import urllib, smtplib
> 
> urls = ("http://website0.net/imalive.asp",
>           "http://website1.net/imalive.asp",
>           "http://website2.net/imalive.asp",
>           "http://website3.net/imalive.asp",
>           "http://website4.net/imalive.asp" 
> <http://website4.net/imalive.asp%22>,)
>          
> count = 0
> for i in urls: 
>     web = urls[count]
>    
>     for site in urllib.urlopen(web):
> 
>         good = "400 Bad Request"
>         bad = "Invalid Hostname"
>         smtpserver = 'mail.authentium.com <http://mail.authentium.com>'
>         RECIPIENTS = ['my at address.com <mailto:my at address.com>']
>         SENDER = 'website-Verification at address.com 
> <mailto:website-Verification at address.com>'
>         mssg = site
> 
>         if good in site:
>             print "OK"
>             text_file = open("test.log", "a")
>             text_file.writelines('sometext : ')
>             text_file.writelines(site)
>             text_file.writelines("\n")
>             text_file.close()
> 
>         elif bad in site:
>               print "NOT OK"
>               text_file = open("test.log", "a")
>               text_file.writelines('metro-ams : ')
>               text_file.writelines(site)
>               text_file.writelines("\n")
>               text_file.close()
>               session = smtplib.SMTP(smtpserver)
>               smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
>               if smtpresult:
>                         errstr = ""
>                         for recip in smtpresult.keys():
>                             errstr = """Could not delivery mail to: %s
> 
>                     Server said: %s
>                     %s
> 
>                     %s""" % (recip, smtpresult[recip][0], 
> smtpresult[recip][1], errstr)
>                         raise smtplib.SMTPException, errstr
> 
>    
>         else:
>               text_file = open("test.log", "a")
>               text_file.writelines('Another type of error occurred : ')
>               text_file.writelines(site)
>               text_file.writelines("\n")
>               text_file.close()
> print web
> count +=1
> print count
> -----------------------------------------------------------------------------------
> 
> Thanks
> 
> 
> -- 
> Arthur DiSegna
> Network Operations Center
> Authentium, Inc.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list