企业怎样做好网站建设,网站哪些功能是PHP做的,建德市建设局网站,番禺网站建设优化推广参考获取PC机公网IP并发送至邮箱
零、找一个发送邮件的邮箱
本文用QQ邮箱为发送邮箱#xff0c;网易等邮箱一般也有这个功能#xff0c;代码也是通用的。 第一步#xff1a;在设置中找到账户#xff0c;找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务#xff0c;点击获…参考获取PC机公网IP并发送至邮箱
零、找一个发送邮件的邮箱
本文用QQ邮箱为发送邮箱网易等邮箱一般也有这个功能代码也是通用的。 第一步在设置中找到账户找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务点击获取授权码。 第二步选择其他方式验证-手机接收验证码验证。 第三步得到授权码复制到下面的代码里。
一、在pycharm里创建py文件代码直接复制进去
# -*- coding: utf-8 -*-
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Headerfrom urllib.request import urlopen
import threading
import datetime
import sys
# smtplib模块主要负责发送邮件是一个发送邮件的动作连接邮箱服务器登录邮箱发送邮件有发件人收信人邮件内容。
# email模块主要负责构造邮件指的是邮箱页面显示的一些构造如发件人收件人主题正文附件等。
# https://blog.csdn.net/Lin_Hv/article/details/110861669这个博客里有更多获取ip的地址链接如https://api.ipify.org/。一旦http://ip.42.pl/raw失效就可以替换。
my_ip urlopen(http://ip.42.pl/raw,timeout5).read()
my_ip my_ip.decode(encodingutf-8)
sender_qq xxxxqq.com # 发送邮箱
receiver [xxxxqq.com,xxxxqq.com] # 接收邮箱
pwd xxxxx # 授权码
def send_email(my_ip,timeNone): host_server smtp.qq.com #qq邮箱smtp服务器mail_title Python自动发送的邮件 #邮件标题mail_content 外网IP{}\n发送时间{}.format(my_ip,time) #邮件正文内容# 初始化一个邮件主体msg MIMEMultipart()msg[Subject] Header(mail_title,utf-8)msg[From] sender_qq# msg[To] Header(测试邮箱,utf-8)msg[To] ;.join(receiver)# 邮件正文内容msg.attach(MIMEText(mail_content,plain,utf-8))smtp SMTP_SSL(host_server) # ssl登录# login(user,password):# user:登录邮箱的用户名。# password登录邮箱的密码像笔者用的是网易邮箱网易邮箱一般是网页版需要用到客户端密码需要在网页版的网易邮箱中设置授权码该授权码即为客户端密码。smtp.login(sender_qq,pwd)# sendmail(from_addr,to_addrs,msg,...):# from_addr:邮件发送者地址# to_addrs:邮件接收者地址。字符串列表[接收地址1,接收地址2,接收地址3,...]或接收地址# msg发送消息邮件内容。一般是msg.as_string():as_string()是将msg(MIMEText对象或者MIMEMultipart对象)变为str。smtp.sendmail(sender_qq,receiver,msg.as_string())# quit():用于结束SMTP会话。smtp.quit()def ip_render():global my_ipglobal timertry:date_time datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S)new_ip urlopen(http://ip.42.pl/raw).read()new_ip new_ip.decode(encodingutf-8)if new_ip ! my_ip:my_ip new_ipsend_email(my_ip,timedate_time)print(IP changed:{} -time:{}.format(my_ip,date_time))else:sys.stdout.write(\rIP doesnt change -time:{}.format(date_time))sys.stdout.flush()except Exception as e:print(Exception:{}.format(e))timer threading.Timer(30, ip_render) # 30s 获取IP一次timer.start()
if __name__ __main__:print(IP:{}.format(my_ip))timer threading.Timer(5, ip_render) # 5s后开始循环线程timer.start()
二、PyInstaller把py文件打包成exe文件
1.下载PyInstaller介绍在cmd里执行命令 pip install pyinstaller。 2.有了待打包脚本后打开cmd切换到该py脚本所在目录执行打包命令pyinstaller find_ip.py 3.完毕后会发现该目录生成了build和dist文件夹可执行exe文件find_ip.exe就在dist文件夹里可以打包build和dist文件夹成一个压缩包。 三、使用方法 解压打包build和dist文件夹后的压缩包到需要获取ip地址的电脑上在dist文件夹最里面有find_ip.exe文件双击出现黑框后敲击回车,出现ip地址就是运行成功了。