python 定时任务去检测服务器端口是否通的实例

yipeiwu_com6年前服务器

如下所示:

import socket
import threading
import time
 
def testconn( host , port ):
  sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sk.settimeout(1)
  try:
    sk.connect((host,port))
    return host+" Server is "+str(port)+" connect"
  except Exception:
    return host+" Server is "+str(port)+" not connect!"
  sk.close()
 
class Test(threading.Thread):
  def __init__(self):
    pass
  def test(self):
    print testconn('172.31.32.3',22)
  def run(self):
    while True:
      #print time.strftime('%Y-%m-%d %H:%M:%S')
      self.test()
      time.sleep(1)
a=Test()
a.run()

以上这篇python 定时任务去检测服务器端口是否通的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python pexpect ssh 远程登录服务器的方法

python pexpect ssh 远程登录服务器的方法

使用了python中的pexpect模块,在测试代码之前,可输入python进入交互界面,输入help('pexpect'),查询是否本地含有pexpect模块。 如果没有,linux系...

python通过邮件服务器端口发送邮件的方法

本文实例讲述了python通过邮件服务器端口发送邮件的方法。分享给大家供大家参考。具体实现方法如下: fromAddress = 'sender@example.com' toAdd...

详解Python程序与服务器连接的WSGI接口

详解Python程序与服务器连接的WSGI接口

了解了HTTP协议和HTML文档,我们其实就明白了一个Web应用的本质就是:     浏览器发送一个HTTP请求;   &nb...

python3实现TCP协议的简单服务器和客户端案例(分享)

利用python3来实现TCP协议,和UDP类似。UDP应用于及时通信,而TCP协议用来传送文件、命令等操作,因为这些数据不允许丢失,否则会造成文件错误或命令混乱。下面代码就是模拟客户端...

python和shell监控linux服务器的详细代码

本文实例为大家分享了python和shell监控linux服务器的具体代码,供大家参考,具体内容如下 1、 shell监控负载 监控原理:使用uptime来获取负载的信息,然后通过字符...