侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

利用Python实现网络测试的脚本分享

Python  /  管理员 发布于 7年前   148

前言

最近同学让我帮忙写一个测试网络的工具。由于工作上的事情,断断续续地拖了很久才给出一个相对完整的版本。其实,我Python用的比较少,所以基本都是边查资料边写程序。

程序的主要逻辑如下:

读取一个excel文件中的ip列表,然后使用多线程调用ping统计每个ip的网络参数,最后把结果输出到excel文件中。

代码如下所示:

#! /usr/bin/env python# -*- coding: UTF-8 -*-# File: pingtest_test.py# Date: 2008-09-28# Author: Michael Field# Modified By:intheworld# Date: 2017-4-17import sysimport osimport getoptimport commandsimport subprocessimport reimport timeimport threadingimport xlrdimport xlwtTEST = [  '220.181.57.217',  '166.111.8.28',  '202.114.0.242',  '202.117.0.20',  '202.112.26.34',  '202.203.128.33',  '202.115.64.33',  '202.201.48.2',  '202.114.0.242',  '202.116.160.33',  '202.202.128.33',]RESULT={}def usage(): print "USEAGE:" print "\t%s -n TEST|excel name [-t times of ping] [-c concurrent number(thread nums)]" %sys.argv[0] print "\t TEST为简单测试的IP列表" print "\t-t times 测试次数;默认为1000;" print "\t-c concurrent number 并行线程数目:默认为10" print "\t-h|-?, 帮助信息" print "\t 输出为当前目录文件ping_result.txt 和 ping_result.xls" print "for example:" print "\t./ping_test.py -n TEST -t 1 -c 10"def div_list(ls,n): if not isinstance(ls,list) or not isinstance(n,int):  return [] ls_len = len(ls) print 'ls length = %s' %ls_len if n<=0 or 0==ls_len:  return [] if n > ls_len:  return [] elif n == ls_len:  return [[i] for i in ls] else:  j = ls_len/n  k = ls_len%n  ### j,j,j,...(前面有n-1个j),j+k  #步长j,次数n-1  ls_return = []  for i in xrange(0,(n-1)*j,j):   ls_return.append(ls[i:i+j])  #算上末尾的j+k  ls_return.append(ls[(n-1)*j:])  return ls_returndef pin(IP): try:  xpin=subprocess.check_output("ping -n 1 -w 100 %s" %IP, shell=True) except Exception:  xpin = 'empty' ms = '=[0-9]+ms'.decode("utf8") print "%s" %ms print "%s" %xpin mstime=re.search(ms,xpin) if not mstime:  MS='timeout'  return MS else:  MS=mstime.group().split('=')[1]  return MS.strip('ms')def count(total_count,I): global RESULT nowsecond = int(time.time()) nums = 0 oknums = 0 timeout = 0 lostpacket = 0.0 total_ms = 0.0 avgms = 0.0 maxms = -1 while nums < total_count:  nums += 1  MS = pin(I)  print 'pin output = %s' %MS  if MS == 'timeout':   timeout += 1   lostpacket = timeout*100.0 / nums  else:   oknums += 1   total_ms = total_ms + float(MS)   if oknums == 0:    oknums = 1    maxms = float(MS)    avgms = total_ms / oknums   else:    avgms = total_ms / oknums    maxms = max(maxms, float(MS))  RESULT[I] = (I, avgms, maxms, lostpacket)def thread_func(t, ipList): if not isinstance(ipList,list):  return else:  for ip in ipList:   count(t, ip)def readIpsInFile(excelName): data = xlrd.open_workbook(excelName) table = data.sheets()[0] nrows = table.nrows print 'nrows %s' %nrows ips = [] for i in range(nrows):  ips.append(table.cell_value(i, 0))  print table.cell_value(i, 0) return ips if __name__ == '__main__': file = 'ping_result.txt' times = 10 network = '' thread_num = 10 args = sys.argv[1:] try:  (opts, getopts) = getopt.getopt(args, 'n:t:c:h?') except:  print "\nInvalid command line option detected."  usage()  sys.exit(1) for opt, arg in opts:  if opt in ('-n'):   network = arg  if opt in ('-h', '-?'):   usage()   sys.exit(0)  if opt in ('-t'):   times = int(arg)  if opt in ('-c'):   thread_num = int(arg) f = open(file, 'w') workbook = xlwt.Workbook() sheet1 = workbook.add_sheet("sheet1", cell_overwrite_ok=True) if not isinstance(times,int):  usage()  sys.exit(0) if network not in ['TEST'] and not os.path.exists(os.path.join(os.path.dirname(__file__), network)):  print "The network is wrong or excel file does not exist. please check it."  usage()  sys.exit(0) else:  if network == 'TEST':   ips = TEST  else:   ips = readIpsInFile(network)  print 'Starting...'  threads = []  nest_list = div_list(ips, thread_num)  loops = range(len(nest_list))  print 'Total %s Threads is working...' %len(nest_list)  for ipList in nest_list:   t = threading.Thread(target=thread_func,args=(times,ipList))   threads.append(t)  for i in loops:   threads[i].start()  for i in loops:   threads[i].join()  it = 0  for line in RESULT:   value = RESULT[line]   sheet1.write(it, 0, line)   sheet1.write(it, 1, str('%.2f'%value[1]))   sheet1.write(it, 2, str('%.2f'%value[2]))   sheet1.write(it, 3, str('%.2f'%value[3]))   it+=1   f.write(line + '\t'+ str('%.2f'%value[1]) + '\t'+ str('%.2f'%value[2]) + '\t'+ str('%.2f'%value[3]) + '\n')  f.close()  workbook.save('ping_result.xls')  print 'Work Done. please check result %s and ping_result.xls.'%file

这段代码参照了别人的实现,虽然不是特别复杂,这里还是简单解释一下。

  •     excel读写使用了xlrd和xlwt,基本都是使用了一些简单的api。
  •     使用了threading实现多线程并发,和POSIX标准接口非常相似。thread_func是线程的处理函数,它的输入包含了一个ip的List,所以在函数内部通过循环处理各个ip。
  •     此外,Python的commands在Windows下并不兼容,所以使用了subprocess模块。

到目前为止,我对Python里面字符集的理解还不到位,所以正则表达式匹配的代码并不够强壮,不过目前勉强可以工作,以后有必要再改咯!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家的支持。


  • 上一条:
    Python编程对列表中字典元素进行排序的方法详解
    下一条:
    python 如何快速找出两个电子表中数据的差异
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客