Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import httplib
- import string
- import time
- import smtplib
- from email.MIMEMultipart import MIMEMultipart
- from email.MIMEBase import MIMEBase
- from email.MIMEText import MIMEText
- from email import Encoders
- import os
- def get_ip():
- # http://imagineerblogs.wordpress.com/2011/09/26/python-script-for-find-your-current-external-ip/
- conn = httplib.HTTPConnection("checkip.dyndns.org")
- conn.request("GET","/index.html")
- r1 = conn.getresponse()
- conn.close()
- if r1.status == 200:
- data1 = r1.read()
- else:
- print 'Error connecting to the server!! Check your internet connection'
- exit()
- startstr = string.find(data1,': ')+2
- endstr = string.find(data1,'</b')
- return data1[startstr:endstr]
- def email_me(current):
- current = current
- gmail_user = "[email protected]"
- gmail_pwd = "put_password_here"
- def mail(to, subject, text, attach):
- msg = MIMEMultipart()
- msg['From'] = gmail_user
- msg['To'] = to
- msg['Subject'] = subject
- msg.attach(MIMEText(text))
- part = MIMEBase('application', 'octet-stream')
- part.set_payload(open(attach, 'rb').read())
- Encoders.encode_base64(part)
- part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(attach))
- msg.attach(part)
- mailServer = smtplib.SMTP("smtp.gmail.com", 587)
- mailServer.ehlo()
- mailServer.starttls()
- mailServer.ehlo()
- mailServer.login(gmail_user, gmail_pwd)
- mailServer.sendmail(gmail_user, to, msg.as_string())
- # Should be mailServer.quit(), but that crashes...
- mailServer.close()
- this_str = str(current)+"\nWell well well. Am i going to put this code on github?"
- current = get_ip()
- last = current
- while True:
- try:
- current = get_ip()
- if current is not last:
- time_stamp = "\nip changed:"+time.ctime()
- print time_stamp
- email_me(current+time_stamp)
- last = current
- else:
- time.sleep(600)
- except Exception,e:
- print(e)
Add Comment
Please, Sign In to add comment