How to automatically notify python after IP changes on public network through raspberry pie
How to use raspberry pie to automatically notify python after public network IP changes? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Obtain the public network IP
Class Getmyip: def getip (self): try: myip = self.visit ("http://www.ip138.com/ip2city.asp") except Exception E1: logging.warning (str (E1)) try: myip = self.visit ("http://www.whereismyip.com") except Exception E2: logging.warning (str (e2)) myip = "So sorryroommates!" Return myip def visit (self,url): opener = urllib2.urlopen (url) # if url = = opener.geturl (): mystr = opener.read () return re.search ('\ d +\.\ d +\.\ d +\.
two。 Send an email
Def send_mail (to_list,sub,content): mail_host= "smtp.163.com" # set server mail_user= "aaa" # username mail_pass= "aaa" # password mail_postfix= "163.com" # suffix me= "hello" + "" msg = MIMEText (content,_subtype='plain' _ charset='utf8') msg ['Subject'] = sub msg [' From'] = me msg ['To'] = " ".join (to_list) try: server = smtplib.SMTP () server.connect (mail_host) server.login (mail_user,mail_pass) server.sendmail (me, to_list, msg.as_string ()) server.close () return True except Exception E: print str (e) return False
3. Check every half hour
If _ _ name__ ='_ _ main__': strLastIP =''logging.info (' Starting message') while True: logging.info ('time.sleep begin') time.sleep (1600) logging.info (' time.sleep end') getmyip = Getmyip () Logging.info ('Getmyip') localip = getmyip.getip () logging.info (' getmyip.getip ='+ localip) if localip = ='So sorryloaded cards' Or localip ='': continue if localip = = strLastIP: logging.info ("The same ip:" + localip) else: logging.info ("localip ok:" + Localip) if send_mail (mailto_list IP + localip, localip): strLastIP = localip logging.info ("send ok") else: logging.warning ("send err")
4. Boot up
Edit / etc/init.d/rc.local
Python / usr/local/work/sendmyip.py &
After reading the above, do you know how to automatically notify python after making public network IP changes through raspberry pie? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!