• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python 多线程实现检测服务器在线情况

python 多线程实现检测服务器在线情况

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了python 多线程,python多线程编程,python多线程实例,python多线程爬虫,python多线程并发等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

需要ping一个网段所有机器的在线情况,shell脚步运行时间太长,用python写个多线程ping吧,代码如下:

#!/usr/bin/python
#coding=utf-8
'''
Created on 2015-8-4
@author: Administrator
'''

import threading,subprocess
from time import ctime,sleep,time
import Queue

queue=Queue.Queue()

class ThreadUrl(threading.Thread):
  def __init__(self,queue):
    threading.Thread.__init__(self)
    self.queue=queue

  def run(self):
    while True:
      host=self.queue.get()
      ret=subprocess.call('ping -c 1 -w 1 '+host,shell=True,stdout=open('/dev/null','w'))
      if ret:
        print "%s is down" % host
      else:
        print "%s is up" % host
      self.queue.task_done()

def main():
  for i in range(100):
    t=ThreadUrl(queue)
    t.setDaemon(True)
    t.start()
  for host in b:
    queue.put(host)
  queue.join()

a=[]
with open('ip.txt') as f:
  for line in f.readlines():
    a.append(line.split()[0])
  #print a

b=['192.168.3.'+str(x) for x in range(1,254)] #ping 192.168.3 网段
start=time()
main()
print "Elasped Time:%s" % (time()-start)

#t2=threading.Thread(target=move,args=('fff',))
#threads.append(t2)

'''
for i in a:
  print ctime()
  ping(i)
  sleep(1)

if __name__ == '__main__':
  for t in range(len(a)):
    #t.setDaemon(True)
    threads[t].start()
    #t.join()
  print "All over %s" % ctime()
'''

</div>

效果如下:

平一个网段只要2.7s左右,够快!!!

再给大家分享一个检测外网服务器的方法及代码

经常使用python检测服务器是否能ping通, 程序是否正常运行(检测对应的端口是否正常)

    以前使用shell脚本的写法如下:

    PINGRET=$( ping www.baidu.com -c 2 | grep "icmp_" );  if [ -z $PINGRET ]; then echo "ping fail"; else echo "ping ok"; fi

    或者

     ping -c 2 www.baidu.com|grep "icmp_" && echo 'ping ok' || echo 'ping fail'

    代码示例:

#!/usr/bin/python
# encoding=utf-8
# Filename: net_is_normal.py
import os
import socket
import subprocess
 
 
#判断网络是否正常
server='www.baidu.com'
#检测服务器是否能ping通,在程序运行时,会在标准输出中显示命令的运行信息
def pingServer(server):
  result=os.system('ping '+server+' -c 2')
  if result:
    print '服务器%s ping fail' % server
  else:
    print '服务器%s ping ok' % server
  print result
 
#把程序输出定位到/dev/null,否则会在程序运行时会在标准输出中显示命令的运行信息 
def pingServerCall(server):
  fnull = open(os.devnull, 'w')
  result = subprocess.call('ping '+server+' -c 2', shell = True, stdout = fnull, stderr = fnull)
  if result:
    print '服务器%s ping fail' % server
  else:
    print '服务器%s ping ok' % server
  fnull.close()
   
#可用于检测程序是否正常,如检测redis是否正常,即检测redis的6379端口是否正常
#检测ssh是否正常,即检测ssh的22端口是否正常
def check_aliveness(ip, port):
  sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sk.settimeout(1)
  try:
    sk.connect((ip,port))
    print 'server %s %d service is OK!' %(ip,port)
    return True
  except Exception:
    print 'server %s %d service is NOT OK!' %(ip,port)
    return False
  finally:
    sk.close()
  return False
   
if __name__=='__main__':
  pingServerCall(server)
  pingServer(server)
  check_aliveness('192.168.230.128', 6379)
</div>

</div>

您可能想查找下面的文章:

  • Python多线程实现同步的四种方式
  • Python多线程实现同步的四种方式
  • Python 多线程实例详解
  • Python 多线程实例详解
  • Python实现的多线程http压力测试代码
  • Python线程指南详细介绍
  • Python线程指南详细介绍
  • python实现多线程抓取知乎用户
  • python 线程的暂停, 恢复, 退出详解及实例
  • 深入理解 Python 中的多线程 新手必看

相关文章

  • Python中使用glob和rmtree删除目录子目录及所有文件的例子
  • python通过pil为png图片填充上背景颜色的方法
  • python去掉字符串中重复字符的方法
  • 在Python中使用模块的教程
  • Python进行数据提取的方法总结
  • Python Web框架Pylons中使用MongoDB的例子
  • 跟老齐学Python之重回函数
  • Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
  • wxpython 最小化到托盘与欢迎图片的实现方法
  • 浅谈Python的Django框架中的缓存控制

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 浅析Python基础-流程控制
    • python字典键值对的添加和遍历方法
    • Python中针对函数处理的特殊方法
    • Python中的字典与成员运算符初步探究
    • Python 列表(List) 的三种遍历方法实例 详解
    • Python中方法链的使用方法
    • 用Python编写web API的教程
    • Python正则表达式实现截取成对括号的方法
    • python实现带验证码网站的自动登陆实现代码
    • Python在图片中添加文字的两种方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有