>  기사  >  백엔드 개발  >  Python으로 구현된 다중 스레드 http 스트레스 테스트 코드

Python으로 구현된 다중 스레드 http 스트레스 테스트 코드

高洛峰
高洛峰원래의
2017-02-10 09:13:112143검색

이 기사의 예에서는 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으로 구현된 멀티 스레드 http 스트레스 테스트 코드와 관련된 더 많은 기사를 보려면 PHP 중국어 웹사이트를 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.