i.have.rewt Posted July 12, 2009 Share Posted July 12, 2009 Hey people I'm new to the forums, hope to learn lots here. In my debut I'd like to share some code I've created. here's the thing.. I'm looking for changes to a website and I hate to have to keep on going back to check it manually..plus there's a bunch of other sites I need to check. This script can be the foundation of some other great implementations. So when there's a change it sends me a text message to my phone. The neat thing is that all I have to do is send the message to an email address like: firstname.lastname@example.org Now this may not apply to all carriers of course but it definitely works for Verizon. I also have this set up to run on a schedule using my home server --since it's on 24/7, I can be sure the script works. When it doesn't I'm notified via the operating system that the job failed...but that's another can of worms. This can be easily modified to do other neat things and I recommend using SSH tunneling whenever you can to improve security. Or if your email service allows it, to use the secure email connection function from Python. (Please please please use Google or python's interactive mode dir/help commands..but I will help if something more difficult arises) Requires: phone (and a carrier that allows sms via email) + Python + email account (that allows SMTP access) #! /bin/usr/env/ python import urllib import smtplib from email.mime.text import MIMEText #Use your email address addr_from = 'email@example.com' pw = 'mypass' #Phone number including area code number = '1112223333' #Change this domain to your carrier's text messaging service domain domain = 'vtext.com' #Your email carrier for using their send mail service SMTPserver = 'smtp.gmail.com' urlToCheck = 'www.change_this.com' ###### You shouldn't need to change anything below this line addr_to = number + '@' + domain cached_name = 'cached.html' try: f = open(cached_name, 'r') except: pass try: lines = f.readlines() except: lines =  try: f.close() except: pass def sendit(msg): # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP(SMTPserver) print s.ehlo() print s.starttls() print s.ehlo() print s.login(addr_from, pw) print s.sendmail(addr_from, [addr_to], msg.as_string()) print s.quit() def main(): spb_page = urllib.urlopen(urlToCheck) wwwlines = spb_page.readlines() if lines and not (lines == wwwlines): txt = 'Testing page has changed!' print txt msg = MIMEText(txt) sendit(msg) f = open(cached_name, 'w') f.writelines(wwwlines) f.close() if not lines: f = open(cached_name, 'w') f.writelines(wwwlines) f.close() if __name__ == '__main__': main() Happy hacking! Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.