[Tutor] Passing HTTP commands through Telnet using Python for web site testing?
Hi, I just started learning Python and would like to try writing a monitoring script. What I would like to do is use Telnet to send a GET via port 80 to a status page in a directory to verify my site is up. Psuedo code: Connect = telnet website.com 80 Send = GET /folder/folder/test.asp HTTP/1.1 Host: website.com Pipe the Response to a file (overwriting it each time): HTTP/1.1 400 Bad Request Server: Server: Microsoft-IIS/5.0 Date: Wed, 16 Nov 2005 20:15:20 GMT X-Powered-By: ASP.NET Connection: close Server: website.com Content-Length: 102 Content-Type: text/html GREP "HTTP/1.1 400 Bad Request" from the file and | append >> it into a log. This tells me the connection was valid. Timestamping the line would be nice. The other error code could be "HTTP/1.1 404 Not Found" if the directory went missing or possibly "Cound not open connection" if the connection could not be made. If the above GREP is found in the file do nothing. If not send an email stating the error. End Anyway, Can someone point me in the right direction on getting the Telnet working? Or if you have a better way to get what I am trying to do accomplished. Is there a Python Telnet module? Thanks in advance ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing HTTP commands through Telnet using Python for web site testing?
So far this is what I have come up with doing some research. import urllib for line in urllib.urlopen('http://mywebsit.com/folder/folder/test.asp'): if '400 Bad Request' in line: text_file = open("mysite.log", "a") text_file.writelines(line) text_file.writelines("\n") text_file.close() This writes the to a new line each time the script is run. Now I have to figure out an "if then else" to write the other possible error codes and appending a timestamp followed by a space each time it runs. Thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] smtplib alternative???
Hello, I found this script to send mail online. It works fine but requires me to enter an mail server. I'm looking for something else that doesn't require and SMTP server. Having to specify a mail server prohibits me from sending to alternate domains. Thanks in advance smtpserver = 'my.mailserver.com' RECIPIENTS = ['[EMAIL PROTECTED]'] SENDER = '[EMAIL PROTECTED]' mssg = SomeVariable 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] smtplib alternative???
Hi Danny, Yes, when sending an email your mail client will always send the email to the mail server specified by the MX record in the authoritive domain. Usually the domain specificed after the @ symbol. The problem with smtplib is that I have to specify the mail server I'm sending email too. What if I wanted to send an email to 3 different people on three different domains hosted by 3 different mail servers? Smtlib prohibits this functionality. Do you see what I mean now...? Thanks for replying...On 11/20/05, Danny Yoo < [EMAIL PROTECTED]> wrote: On Sat, 19 Nov 2005, Adisegna wrote:> I found this script to send mail online. It works fine but requires me> to enter an mail server. I'm looking for something else that doesn't> require and SMTP server. Having to specify a mail server prohibits me > from sending to alternate domains.Hi Adisegna,I've always assumed that emails have to talk with some SMTP server.RFC821 seems to confirm this: http://www.faqs.org/rfcs/rfc821.htmlso what you're asking, to be able to send mail without an SMTP server, maynot be possible. There is an 'smtpd' module that comes with Python: http://www.python.org/doc/lib/module-smtpd.htmlI'm also not sure I understand the reason you're trying to avoid talkingto an outside smtp server. But again, I'm unfamiliar enough with how theSMTP email protocol really works that perhaps I'm just overlooking something.-- Arthur DiSegnaNetwork Operations CenterAuthentium, Inc. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Website monitoring program.
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 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",) # 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 good = "400 Bad Request" bad = "Invalid Hostname" smtpserver = 'mail.authentium.com' RECIPIENTS = ['[EMAIL PROTECTED]'] SENDER = '[EMAIL PROTECTED]' 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@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Website monitoring program.
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? 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",) 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' RECIPIENTS = ['[EMAIL PROTECTED]'] SENDER = '[EMAIL PROTECTED]' 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 DiSegnaNetwork Operations CenterAuthentium, Inc. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Website monitoring program.
I need to add the count statement to give urllib access the tuple of urls. urllib needs to be given a different value each time in order to check all the urls. This is the only way I could get the value (web) in urllib to change each time. I tried indenting the count statement and it runs without error but still only makes one pass. Thanks Message: 1 Date: Sun, 20 Nov 2005 22:26:10 -0500 From: Kent Johnson <[EMAIL PROTECTED]> Subject: Re: [Tutor] Website monitoring program. Cc: tutor@python.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=ISO-8859-1; format=flowed 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#SECTION00620 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 = ['[EMAIL PROTECTED] [EMAIL PROTECTED]>']> SENDER = '[EMAIL PROTECTED]> [EMAIL PROTECTED]>'> 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 -- Arthur DiSegnaNetwork Operations CenterAuthentium, Inc. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: Website monitoring program.
This worked for me. Thanks! -- for web in urls: for site in urllib.urlopen(web): --- 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#SECTION00620 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 = ['[EMAIL PROTECTED] [EMAIL PROTECTED]>']> SENDER = '[EMAIL PROTECTED]> [EMAIL PROTECTED]>'> 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 -- Arthur DiSegnaNetwork Operations CenterAuthentium, Inc. -- Arthur DiSegnaNetwork Operations CenterAuthentium, Inc. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor