获取当前机器IP地址并发信通知
# If you want to use this python script,
# please follow the instructions below.
# 1. Make sure you fill in the correct SMTP server domain
# 2. Make sure that the SMTP service is enabled in you email address (send-side).
# 3. Make sure you fill in the correct SMTP token about your email address (send-side).
import smtplib
import requests
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Set Email server and address info
smtp_server_domain = r'Please fill in your SMTP server domain here'
from_address = r'Please fill in the sender email address here'
from_address_smtp_token = r'Please fill in the sender email SMTP token here'
to_address = r'Please fill in the recipient email address here'
# Request below url to get public IP address
request_time = datetime.now()
request_url = 'http://members.3322.org/dyndns/getip'
public_ip_address = requests.get(request_url).text.strip()
# Organize Email message
email_msg = MIMEMultipart()
email_msg['From'] = from_address
email_msg['To'] = to_address
email_msg['Subject'] = r'{} Public IP Address is {}'.format(request_time, public_ip_address)
email_msg_body = r'{} Public IP Address is {}'.format(request_time, public_ip_address)
email_msg.attach(MIMEText(email_msg_body, 'plain'))
# Sent Email
email_server = smtplib.SMTP(smtp_server_domain, 25)
email_server.starttls()
email_server.login(from_address, from_address_smtp_token)
email_server.sendmail(from_address, to_address, email_msg.as_string())
email_server.quit()
# Echo execute result
print('Service Execute Success!\nService Name: Get Public IP Address\nExecute Time: {}'.format(datetime.now()))