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正则表达式re模块详解

快速入门 import re pattern = 'this' text = 'Does this text match the pattern?' match = re...

Python实现时间序列可视化的方法

Python实现时间序列可视化的方法

时间序列数据在数据科学领域无处不在,在量化金融领域也十分常见,可以用于分析价格趋势,预测价格,探索价格行为等。 学会对时间序列数据进行可视化,能够帮助我们更加直观地探索时间序列数据,寻...

详解用python生成随机数的几种方法

今天学习了用python生成仿真数据的一些基本方法和技巧,写成博客和大家分享一下。     本篇博客主要讲解如何从给定参数的的正态分布/均匀分布...

Python Pickle 实现在同一个文件中序列化多个对象

也是看别人代码才知道可以打开一个文件就可以把多个对象序列化到这个文件中。 with open('../raw_data/remap.pkl', 'wb') as f: pickle...

Python利用PyExecJS库执行JS函数的案例分析

Python利用PyExecJS库执行JS函数的案例分析

  在Web渗透流程的暴力登录场景和爬虫抓取场景中,经常会遇到一些登录表单用DES之类的加密方式来加密参数,也就是说,你不搞定这些前端加密,你的编写的脚本是不可能...