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

相关文章

TensorFlow saver指定变量的存取

TensorFlow saver指定变量的存取

今天和大家分享一下用TensorFlow的saver存取训练好的模型那点事。 1. 用saver存取变量; 2. 用saver存取指定变量。 用saver存取变量。 话不多说,先上...

python删除列表元素的三种方法(remove,pop,del)

remove 删除单个元素,删除首个符合条件的元素,按值删除,返回值为空 List_remove = [1, 2, 2, 2, 3, 4] print(List_remove.re...

Python使用itchat模块实现群聊转发,自动回复功能示例

本文实例讲述了Python使用itchat模块实现群聊转发,自动回复功能。分享给大家供大家参考,具体如下: 1.itchat自动把好友发来的消息,回复给他 仅能实现自动回复 原文给 好友...

python如何使用jt400.jar包代码实例

这篇文章主要介绍了python如何使用jt400.jar包代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 jt400...

Python实现的简单读写csv文件操作示例

Python实现的简单读写csv文件操作示例

本文实例讲述了Python实现的简单读写csv文件操作。分享给大家供大家参考,具体如下: python中有一个读写csv文件的包,直接import csv即可 新建test.csv 1....