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程序设计有所帮助。

相关文章

pycharm 使用心得(四)显示行号

pycharm 使用心得(四)显示行号

在PyCharm 里,显示行号有两种办法: 1,临时设置。右键单击行号处,选择 Show Line Numbers。 但是这种方法,只对一个文件有效,并且,重启PyCharm 后消失。...

在Django中创建动态视图的教程

在我们的`` current_datetime`` 视图范例中,尽管内容是动态的,但是URL ( /time/ )是静态的。 在 大多数动态web应用程序,URL通常都包含有相关的参数。...

使用virtualenv创建Python环境及PyQT5环境配置的方法

使用virtualenv创建Python环境及PyQT5环境配置的方法

一、写在前面   从学 Python 的第一天起,我就知道了使用 pip 命令来安装包,从学习爬虫到学习 Web 开发,安装的库越来越多,从 requests 到 lxml,从 Djan...

Django使用Jinja2模板引擎的示例代码

Django使用Jinja2模板引擎的示例代码

Jinja2模板引擎 安装Jinja2 :pip install jinja2,在应用目录下添加jinja2_env.py设定环境变量。 from django.contrib.s...

PyQt5使用QTimer实现电子时钟

PyQt5使用QTimer实现电子时钟

本文用 PyQt5 的QTimer类的两种方式实现电子时钟,供大家参考,具体内容如下 【效果图】 【知识点】 QTimer类提供了定时器信号/槽和单触发定时器。 它在内部使用定时器事件...