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

yipeiwu_com6年前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 urls.py的三种配置写法实例详解

urls.py的配置写法一般有三种方式。 1. 第一种是导入视图的方式,就是 The Django Book 里面样例的写法: from blog.views import inde...

python 导入数据及作图的实现

我们经常需要导入数据,按列提取 XY作图 方法一、 filename='/home/res/user/csluo/test.txt' #将文件名赋值为变量 X,...

Python利用openpyxl库遍历Sheet的实例

方法一,利用 sheet.iter_rows() 获取 Sheet1 表中的所有行,然后遍历 import openpyxl wb = openpyxl.load_workbook...

python用来获得图片exif信息的库实例分析

本文实例讲述了python用来获得图片exif信息的库用法。分享给大家供大家参考。具体分析如下: exif-py是一个纯python实现的获取图片元数据的python库,官方下载地址:...

Python中filter与lambda的结合使用详解

filter是Python的内置方法。 官方定义是: filter(function or None, sequence) -> list, tuple, or string...