python获取外网IP并发邮件的实现方法
第一步:通过ip138来爬取外网ip
第二步:通过python的smtplib模块和email来发送邮件,具体用法去网上搜索,
下面是代码示例:
#!/usr/bin/env python #coding:utf-8 import urllib2 import re import smtplib from email.MIMEText import MIMEText from email.Header import Header ########################################## #get ip address url = "http://1212.ip138.com/ic.asp" url_op = urllib2.urlopen(url) url_content = url_op.read() ip_content = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',url_content) ipcode = ''.join(ip_content) ################################################# # Setting mail-server, etc mail_host="smtp.sina.com" mail_user="ckl" mail_pass="woXXXX" mail_postfix="sina.com" ################################################# def send_mail(to_list, sub, content): me = mail_user + "<" + mail_user + "@" + mail_postfix + ">" msg = MIMEText(content) msg['Subject'] = sub msg['From'] = me msg['To'] = ";".join(to_list) try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user, mail_pass) s.sendmail(me, to_list, msg.as_string()) s.close() return True except Exception, e: print str(e) return False ################################################# # Main process if __name__ == "__main__": mailto_list=["[email protected]"] if send_mail(mailto_list, "你的最新IP地址", ipcode): print "Send success!" else: print "Send failed!"
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16