Python实现的多线程http压力测试代码

yipeiwu_com5年前Python基础

本文实例讲述了Python实现的多线程http压力测试代码。分享给大家供大家参考,具体如下:

# Python version 3.3
__author__ = 'Toil'
import sys, getopt
import threading
def httpGet(url, file):
  import http.client
  conn = http.client.HTTPConnection(url)
  conn.request("GET", file)
  r = conn.getresponse()
  #print(r.getheaders())
  while not r.closed:
    r.read(200)
  conn.close()
def Usage():
  print('''
  Options are:
  -c concurrency Number of multiple requests to make
  -u host     The host
  -f file     File on web
  Example: httpget.py -c 100 -u www.example.com -f /
  ''')
if __name__ == '__main__':
  opts, args = getopt.getopt(sys.argv[1:], "hc:u:f:")
  global u, c, f
  for op, value in opts:
    if op == '-c':
      c = int(value)
    elif op == '-u':
      u = value
    elif op == '-f':
      f = value
    elif op == '-h':
      Usage()
      sys.exit(0)
    else:
      sys.exit(0)
  threads = []
  times = c
  print('Test for ', u, f)
  print('waiting...')
  for i in range(0, times):
    t = threading.Thread(target=httpGet(u, f))
    threads.append(t)
  for i in range(0, times):
    threads[i].start()
  for i in range(0, times):
    threads[i].join()

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python URL操作技巧总结》、《Python Socket编程技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

详解python实现小波变换的一个简单例子

详解python实现小波变换的一个简单例子

最近工作需要,看了一下小波变换方面的东西,用python实现了一个简单的小波变换类,将来可以用在工作中。 简单说几句原理,小波变换类似于傅里叶变换,都是把函数用一组正交基函数展开,选取不...

python Event事件、进程池与线程池、协程解析

Event事件 用来控制线程的执行 出现e.wait(),就会把这个线程设置为False,就不能执行这个任务; 只要有一个线程出现e.set(),就会告诉Event对象,把有e.wai...

NumPy排序的实现

numpy.sort()函数 该函数提供了多种排序功能,支持归并排序,堆排序,快速排序等多种排序算法 使用numpy.sort()方法的格式为: numpy.sort(a,axis,k...

Python数据结构与算法之图结构(Graph)实例分析

Python数据结构与算法之图结构(Graph)实例分析

本文实例讲述了Python数据结构与算法之图结构(Graph)。分享给大家供大家参考,具体如下: 图结构(Graph)——算法学中最强大的框架之一。树结构只是图的一种特殊情况。 如果我们...

python调用摄像头显示图像的实例

如下所示: import cv2 import numpy as np bins = np.arange(256).reshape(256,1) def hist_curve(i...