Python: IP Address Change sent to mailbox

Source: Internet
Author: User
Tags email account to domain

(Ah, I did something two months ago. I threw an article at the end of November because I was lazy and did not write anything this month)
Today, I am mainly sharing a tool that allows users to remotely control/access hosts with public IP addresses through Web Services (most ADSL users have public IP addresses.
This idea is due to the fact that even a relative wants to visit his warehouse to monitor the camera status. The monitoring device has a local Web Service for remote access, previously, dynamic IP addresses bound to domain names using peanut shells were always invalid, and the fire was so hot that they didn't need to be used anymore, because there was access from China Telecom ADSL and dynamic public IP addresses were obtained, so I thought if I could use a tool to send the IP address of the server to his mailbox, it would be okay. Just recently I played python And I located the "Development Tool" on it, this was my first attempt to touch python. As before, the results soon came out.
I searched for two pieces of information. Python obtained the IP address of the local machine, and another one sent an email from Python. Then, it turned:Send the IP address to the specified mailbox when the IP address of the local machine changesIt is very easy to implement. I want to explain why this "send when IP address changes" is required, because the public IP address obtained by China Telecom ADSL will change every n days (n = ?, It seems that the system will also change), and regular sending will be stupid (there will be invalid emails, why will the IP address not be changed), so we can perform an IP detection ~~

See belowCodeContains two files. One is the configuration file.Config. pyAnd the other is the masterProgramPartSendip. py

Config. py:

# Coding = UTF-8
Smtpserver = ' Smtp.126.com '   # SMTP server address of the email sending account
Smtpport = ' 25 '   # SMTP server sending port of the email sending account
Smtpuser = ' XXXX@126.com '   # Account name for email sending
Smtppwd = ' Xxxxxx '   # Email sending account password. Here is an example of "X", but you have to enter the password.
Sendto = ' XXXX@qq.com '   # Recipient email address

Contains an email sending account and an email Receiving address (this is the "specified email address" mentioned above). Of course, the two can be the same, here, we use the 126 email account as an example.

Sendip. py:

# Coding = UTF-8
Import Smtplib, config, email, sys, socket, threading
From Email. Message Import Message

Def Connect ():
Try :
Server = Smtplib. SMTP (config. smtpserver, config. smtpport)
Server. EHLO ()
Server. login (config. smtpuser, config. smtppwd)
Return Server
Except Exception:
Print   ' Connect SMTP server failed! '

Def Sendmessage (server, to, subj, content ):
MSG = Message ()
MSG [ ' Mime-version ' ] = ' 1.0 '
MSG [ ' From ' ] = Config. smtpuser
MSG [ ' To ' ] = To
MSG [ ' Subject ' ] = Subj
MSG. set_payload (content)
Try :
Failed = Server. Sendmail (config. smtpuser, to, STR (MSG ))
Except Exception, EX:
Print Exception, ex
Print   ' Error-send failed '
Else :
Print   ' Send succeed! '

# Check the IP address once every 10 s. You can adjust the IP address size accordingly.
Def Checkip ():
Global Text
Print Text
Ipnow = STR (socket. gethostbyname_ex (socket. gethostname ()))
Print Ipnow
If Text ! = Ipnow:
Text = Ipnow
Server = Connect ()
Sendmessage (server, to, subj, text)
T = Threading. Timer ( 10.0 , Checkip)
T. Start ()

If   _ Name __ = " _ Main __ " :
To = Config. sendto
Subj = ' IP changed '  
Text = STR (socket. gethostbyname_ex (socket. gethostname ())) # This is the IP address of the local machine.
Server = Connect ()
Sendmessage (server, to, subj, text)
T = Threading. Timer ( 10.0 , Checkip)
T. Start ()

The obtained local IP addresses are all IP addresses. Of course, you can write several lines of code to separate the public network ~~

 

Then let sendip. py run on startup (remember to install the python runtime environment first), as long as the Remote Desktop is in the allowed state, you can remotely control it at any time.
Close! (November -_-!)

(Test environment: Windows XP SP3, Python 2.52)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.