一些学习成果的分享。

Python+Windows任务计划程序+服务器PHP页面实现邮件发送IP地址

实验室公网IP偶尔抽风变化,远程桌面会偶尔失效,因此编写该程序。

import requests
#import threading
from bs4 import BeautifulSoup
from smtplib import SMTP
from email.header import Header
from email.mime.text import MIMEText
import time

def get_ip():
   req = requests.get('http://ip.onlyhao.cn', timeout=5)
    req.encoding = "utf-8"
    html = req.text
    soup = BeautifulSoup(req.text, features="html.parser")
    com = soup.find("body")
    res = com.text.strip()
    return res

def mail(content):
    # 发送者的邮箱地址
    sender = '发送者的邮箱地址@qq.com'
    # 接收者的邮箱地址
    receivers = ['接收者的邮箱地址@foxmail.com']  
    # content是要发送的内容
    message = MIMEText(content, _subtype='plain', _charset='utf-8')
    # 邮件的发送者
    message['From'] = Header('IP_BOT', 'utf-8')
    # 邮件的接收者
    message['To'] = Header('IP_RE', 'utf-8')
    # 邮件的标题
    message['Subject'] = Header('TMD教研室电脑的IP又变了', 'utf-8') 
    smtper = SMTP('smtp.qq.com')
    # QQ邮箱smtp的授权码
    smtper.login(sender, 'QQ邮箱smtp的授权码')  
    smtper.sendmail(sender, receivers, message.as_string())

    
#用一个txt存放上次获取到的值
f = open('ip.txt', 'r', encoding='gbk')
old_ip = f.read()
f.close()
print(get_ip())
print(old_ip)

if old_ip != get_ip():
    print('budengyu')
    old_ip = get_ip()
    mail(old_ip)
    with open('ip.txt', 'w') as f:
        f.write(old_ip)

其中http://ip.onlyhao.cn 网页部署在远端服务器,网站根目录下放置index.php,内容如下,效果是返回utf-8格式的ip地址。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>IP</title>
</head>

<body>
<?php

//$ip = $_SERVER["REMOTE_ADDR"];
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
    $ip = getenv("HTTP_CLIENT_IP");
} else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
    $ip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
    $ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
    $ip = $_SERVER['REMOTE_ADDR'];
else
    $ip = "unknown";
print_r($ip);

?>


</body>
</html>

经过检验,以上代码在面对断网更换ip时有很大局限性,因此后续添加了断网自动重新登陆的功能。代码如下:

# 导入相关库
from email.mime.text import MIMEText

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import requests
#import threading
from bs4 import BeautifulSoup
from smtplib import SMTP
from email.header import Header
from email.mime.text import MIMEText
#import os

#from selenium.webdriver import ActionChains #selenium的动作方法
# 实例化浏览器对象,这里用的是谷歌浏览器,将下载的chromediver.exe放到python3.x环境的目录下
# 下载和谷歌版本对应的chromedirver.exe版本

def get_ip():
    req = requests.get('http://ip.onlyhao.cn', timeout=5)
    req.encoding = "utf-8"
    html = req.text
    soup = BeautifulSoup(req.text, features="html.parser")
    com = soup.find("body")
    res = com.text.strip()
    return res

f = open('ip.txt', 'r', encoding='utf-8')
old_ip = f.read()
f.close()
print(get_ip())
print(old_ip)

if old_ip != get_ip():
    bro = webdriver.Chrome()

    # 让浏览器发起一个指定url对应请求
    bro.get('http://10.253.0.237')

    # 如果定位的相关登录操作标签在iframe标签中,必须使用下面操作,login_frame是iframe标签的id属性值
    #bro.switch_to.frame('login_frame')
    # 再定位到要操作的位置标签
    #a_tag=bro.find_element_by_id('switcher_plogin')
    #a_tag.click() #点击

    # 定位到相应的输入框(账号和密码)
    username_tag = bro.find_element(By.ID, "username")
    password_tag = bro.find_element(By.ID, "password")
    sleep(1)

    # send_keys()里面输入你的账号和密码
    username_tag.send_keys('学号')
    sleep(1)
    password_tag.send_keys('密码')
    sleep(1)

    # 定位到登录按钮
    #btn=bro.find_element_by_id('school-login')
    btn = bro.find_element(By.ID, "school-login")
    btn.click() #点击
    sleep(1)

    # 关闭浏览器
    sleep(5)
    bro.quit()





def mail(content):
    # 发送者的邮箱地址
    sender = '发送者的邮箱地址@qq.com'
    # 接收者的邮箱地址
    receivers = ['接收者的邮箱地址@foxmail.com']
    # content是要发送的内容
    message = MIMEText(content, _subtype='plain', _charset='utf-8')
    # 邮件的发送者
    message['From'] = Header('IP_BOT', 'utf-8')
    # 邮件的接收者
    message['To'] = Header('IP_RE', 'utf-8')
    # 邮件的标题
    message['Subject'] = Header('TMD教研室电脑的IP又变了', 'utf-8')
    smtper = SMTP('smtp.qq.com')
    # QQ邮箱smtp的授权码
    smtper.login(sender, 'QQ邮箱smtp的授权码')
    smtper.sendmail(sender, receivers, message.as_string())


# 用一个txt存放上次获取到的值
f = open('ip.txt', 'r', encoding='utf-8')
old_ip = f.read()
f.close()
print(get_ip())
print(old_ip)

if old_ip != get_ip():
    print('budengyu')
    old_ip = get_ip()
    # ff = os.popen('C:\IP_BOT\login.py')
    # ff = os.system('C:\IP_BOT\login.py')
    # ress = ff.readlines()
    # ress
    mail("新的IP为:" + old_ip)
    with open('ip.txt', 'w', encoding='utf-8') as f:
        f.write(old_ip)

添加新评论