Python实现的HTTP并发测试完整示例

所属分类: 脚本专栏 / python 阅读数: 1158
收藏 0 赞 0 分享

可修改变量thread_count指定最大的并发数量,即线程的数量。

完成之后,打印输出失败的次数,以及开始时间和结束时间,单位是毫秒。

主要是学习一下Python,仅供参考。

#!/usr/bin/python3

import sys, time, json, _thread
import http.client, urllib.parse

thread_count = 100  #并发数量
now_count = 0
error_count = 0
begin_time = ''

lock_obj = _thread.allocate()

def test_http_engine():
  global now_count
  global error_count
  global thread_count
  global begin_time
  conn = None
  if now_count == 0:
    begin_time = int(round(time.time() * 1000))
  try:
    conn = http.client.HTTPConnection("192.168.1.1", 80)
    conn.request('GET', '/')

    response = conn.getresponse()
    data = response.read()
    print (data)

    if json.dumps(response.status) != '200':
      error_count += 1;
      print ('error count: ' + str(error_count))

    sys.stdout.flush()
    now_count += 1
    if now_count == thread_count:
      print ('### error count: ' + str(error_count) + ' ###')
      print ('### begin time : ' + str(begin_time))
      print ('### end time  : ' + str(int(round(time.time() * 1000))))

  except Exception as e:
    print (e)
  finally:
    if conn:
      conn.close()

def test_thread_func():
  global now_count
  global lock_obj
  cnt = 0

  lock_obj.acquire()
  print ('')
  print ('=== Request: ' + str(now_count) + ' ===')

  cnt += 1
  test_http_engine()
  sys.stdout.flush()
  lock_obj.release()


def test_main():
  global thread_count
  for i in range(thread_count):
    _thread.start_new_thread(test_thread_func, ())

if __name__=='__main__':
  test_main()
  while True:
    time.sleep(5)
更多精彩内容其他人还在看

Python实现图像几何变换

这篇文章主要介绍了Python实现图像几何变换的方法,实例分析了Python基于Image模块实现图像翻转、旋转、改变大小等操作的相关技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的urllib模块使用详解

这篇文章主要介绍了Python中的urllib模块使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Python的多态性实例分析

这篇文章主要介绍了Python的多态性,以实例形式深入浅出的分析了Python在面向对象编程中多态性的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

python生成IP段的方法

这篇文章主要介绍了python生成IP段的方法,涉及Python文件读写及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python操作redis的方法

这篇文章主要介绍了python操作redis的方法,包括Python针对redis的连接、设置、获取、删除等常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python妹子图简单爬虫实例

这篇文章主要介绍了python妹子图简单爬虫,实例分析了Python爬虫程序所涉及的页面源码获取、进度显示、正则匹配等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

分析用Python脚本关闭文件操作的机制

这篇文章主要介绍了分析用Python脚本关闭文件操作的机制,作者分Python2.x版本和3.x版本两种情况进行了阐述,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现搜索指定目录下文件及文件内搜索指定关键词的方法

这篇文章主要介绍了python实现搜索指定目录下文件及文件内搜索指定关键词的方法,可实现针对文件夹及文件内关键词的搜索功能,需要的朋友可以参考下
收藏 0 赞 0 分享

python中getaddrinfo()基本用法实例分析

这篇文章主要介绍了python中getaddrinfo()基本用法,实例分析了Python中使用getaddrinfo方法进行IP地址解析的基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python查找指定具有相同内容文件的方法

这篇文章主要介绍了python查找指定具有相同内容文件的方法,涉及Python针对文件操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多