能不能写一个不停监测一个 ip,上线和下线用 srver 酱关联微信提醒的小程序。

2018-11-18 20:45:18 +08:00
 zhujunhan7
2667 次点击
所在节点    Python
7 条回复
Event
2018-11-18 21:25:38 +08:00
你好 可以 再见
annoy1309
2018-11-18 21:32:45 +08:00
能,不难
AlisaDestiny
2018-11-18 22:19:49 +08:00
easy.具体需求说下。
wdc63
2018-11-18 22:34:01 +08:00
'''python
#pip install requests
import subprocess, platform
import requests
import time
def ping(host):
"""
Returns True if host responds to a ping request
"""
import subprocess, platform

# Ping parameters as function of OS
ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1"
args = "ping " + " " + ping_str + " " + host
need_sh = False if platform.system().lower()=="windows" else True

# Ping
return subprocess.call(args, shell=need_sh) == 0

on_state = 1
off_state = 1


def watch(host):
global on_state, off_state
while True:
get_ping = ping(host)
if get_ping:
##如果检测到在线,且离线状态为真(off_state>0),发送上线信号,并将离线状态重置为假(=0 )
if off_state > 0:
off_state = 0
requests.get("https://sc.ftqq.com/[SCKEY(登入后可见)].send?text=服务器上线!")
else:
on_state += 1
else:
##如果检测到连续离线五次,且在线状态为真(off_state>0),发送离线信号,并将在线状态重置为假(=0 )
if on_state > 0 and off_state >= 5:
on_state = 0
requests.get("https://sc.ftqq.com/[SCKEY(登入后可见)].send?text=服务器离线!")
else:
off_state += 1
time.sleep(1)

watch('www.baidu.com')
'''
wdc63
2018-11-18 22:36:56 +08:00
https://gist.github.com/wdc63/869c104ac43841f20b25a40b4dd3b7d9

不会插代码,晕死,拿去试试,随便写了,没有试运行过
Tink
2018-11-19 00:33:11 +08:00
很简单
winglight2016
2018-11-19 12:58:05 +08:00
8 年以前写过一个 Android app 就是做这个事儿的,好久没维护了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/509025

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX