监听服务器的端口
Socket 方式
import socket
def connecthost(ip,port):
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(2)
try:
sk.connect((ip,port))
return True
except Exception as e:
return False
sk.close()
ret = connecthost('baidu.com',80)
print(ret)
# 返回 True | False
发送Telegram脚本
PY版本:python3.6.8
pip3 install pyTelegramBotAPI
# coding: utf8
import telebot
import socket
import time
IP_RANGE = {
'Facebook': {
'address': 'baidu.com',
'port': 80
},
'WhatApp': {
'address': 'mmpasdfasdfasfasdfa.com',
'port': 80
},
}
class CheckHostPort():
def __init__(self):
self.TG_TOKEN = "2027160443:xxxxxxxx"
self.TG_ID = "-xxxxx"
def toTelegram(self, text):
try:
bot = telebot.TeleBot(self.TG_TOKEN)
bot.send_message(self.TG_ID, text, parse_mode="Markdown")
except:
print("\033[0;37;41m\tNetowork Error!!!\033[0m")
@staticmethod
def connectHost(ip,port):
try:
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(2)
try:
sk.connect((ip,port))
return True
except Exception as e:
return False
sk.close()
except:
print("\033[0;37;41m\tNetowork Error!!!\033[0m")
def main():
err = []
C = CheckHostPort()
for k,v in IP_RANGE.items():
# Get ip ,port from Array
ip = str(v['address'])
port = int(v['port'])
# check time
s_time = time.strftime('%Y-%m-%d %X', time.localtime())
# Test active, Return True | False
ret = C.connectHost(ip, port)
# Print
print("{0} {1} 检测: {2}, 端口: {3}".format(s_time,ret,ip,port))
# If it can't connect, add alter list
if not ret:
err.append(k)
if len(err) > 0:
s_time = time.strftime('%Y-%m-%d %X', time.localtime())
s_content = ','.join(err)
text = '''
```python
警告!!!\n检测时间: {0}\n检测主机: {1}\n检测级别: 挂比了
```
'''.format(s_time, s_content)
C.toTelegram(text)
if __name__ == '__main__':
main()
Last updated