[Tutor] Website monitoring program.

Kent Johnson kent37 at tds.net
Sun Nov 20 22:24:35 CET 2005


Adisegna wrote:
> 
> I guess I should keep the same subject line (above) from now on since my 
> last few posts (urllib, smtplib) are about the same program. 

My preference is to start a new subject for each new question.
> 
> My question is how to use a loop to go through a tuple of URLs. Please 
> feel free to suggest an easier way to do the same thing.
> 
> Here is my rudimentary program so far.
> 
> -------------------------------------------------------
> 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">,)
>          
> # I added these just to make the loop work and be more readable
> 
> qa = urls[0]
> n4k = urls[1]
> metro = urls[4]
> 
> for i in urls:   # <-- this obviously doens't work because I have to 
> specify a value "metro" below
> 
>     for site in urllib.urlopen(metro):  #<----- What can I use here to 
> loop through the 'urls' tupple

I'm not sure I understand but I think what you want is

for url in urls:  # url will take on each value from urls
  site = urllib.urlopen(url).read()  # site will have the entire text from the page you opened

You might also be interested in EDDIE which seems to support building this kind of monitoring app fairly easily.
http://eddie-tool.net/doc/manual.html

Kent

> 
>         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()
>                
> -----------------------------------------------------------------------------------
> 
> Thanks
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list