Python实现的多线程http压力测试代码
本文实例讲述了Python实现的多线程http压力测试代码。分享给大家供大家参考,具体如下:
#Pythonversion3.3
__author__='Toil'
importsys,getopt
importthreading
defhttpGet(url,file):
importhttp.client
conn=http.client.HTTPConnection(url)
conn.request("GET",file)
r=conn.getresponse()
#print(r.getheaders())
whilenotr.closed:
r.read(200)
conn.close()
defUsage():
print('''
Optionsare:
-cconcurrencyNumberofmultiplerequeststomake
-uhostThehost
-ffileFileonweb
Example:httpget.py-c100-uwww.example.com-f/
''')
if__name__=='__main__':
opts,args=getopt.getopt(sys.argv[1:],"hc:u:f:")
globalu,c,f
forop,valueinopts:
ifop=='-c':
c=int(value)
elifop=='-u':
u=value
elifop=='-f':
f=value
elifop=='-h':
Usage()
sys.exit(0)
else:
sys.exit(0)
threads=[]
times=c
print('Testfor',u,f)
print('waiting...')
foriinrange(0,times):
t=threading.Thread(target=httpGet(u,f))
threads.append(t)
foriinrange(0,times):
threads[i].start()
foriinrange(0,times):
threads[i].join()
更多关于Python相关内容感兴趣的读者可查看本站专题:《PythonURL操作技巧总结》、《PythonSocket编程技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
