本帖最后由 waterfis 于 2022-12-8 10:01 编辑
由于公司拉专线,但没资源给固定IP,IP经常两三天一变,因此弄了个每天自动发IP到钉钉群,方便大家远程或者其他用途,欢迎大家讨论或补充
感谢坛友提点,已优化成当IP变动,才发送ip,每半小时检测一次IP变动
[Python] 纯文本查看 复制代码 #! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import requests
import json
from urllib.request import urlopen
from json import load
import time
def msg(text):
token ="钉钉群的token"
headers = {'Content-Type': 'application/json;charset=utf-8'} # 请求头
api_url = "https://oapi.dingtalk.com/robot/send?access_token=" + token
json_text = {
"msgtype": "text", # 信息格式
"text": {
"content": text
}
}
print(json_text)
# 发送并打印信息
r = requests.post(api_url, json.dumps(json_text), headers=headers).content
print(r)
#获取IP
def get_ip():
my_ip = resp = urlopen('http://httpbin.org/ip')
#http://ip.42.pl/raw
#https://api.ipify.org/?format=json
#http://jsonip.com
return str(my_ip.read(),"utf-8")
def start():
currIP = "0.0.0.0"
while True:
ip_result = get_ip()
ip_json = json.loads(ip_result) #格式化返回结果
ip = ip_json["origin"]
if(ip != currIP):
currIP = ip #判断ip是不是一致,不一致就发送最新ip信息
msg(ip_result)
time.sleep(1800)
if __name__ == "__main__":
start() |