python建立一个http请求_在python中为每个HTTP请求创建新的TCP连接

'''

Traffic Generator Script:

Here I have used IP Aliasing to create multiple clients on single vm machine.

Same I have done on server side to create multiple servers. I have around 50 clients and 10 servers

'''importmultiprocessingimporturllib2importrandomimportmyurllist#list of all destination urls for all 10 serversimporttimeimportsocbindtry#script that binds various virtual/aliased client ips to the scriptresponse_time=[]#some shared variableserror_count=multiprocessing.Value('i',0)defsend_request3():#function to send requests from alias client ip 1opener=urllib2.build_opener(socbindtry.BindableHTTPHandler3)#bind to alias client ip1try:tstart=time.time()foriinrange(myurllist.url):x=random.choice(myurllist.url[i])opener.open(x).read()print"file downloaded:",x

response_time.append(time.time()-tstart)excepturllib2.URLError,e:error_count.value=error_count.value+1defsend_request4():#function to send requests from alias client ip 2opener=urllib2.build_opener(socbindtry.BindableHTTPHandler4)#bind to alias client ip2try:tstart=time.time()foriinrange(myurllist.url):x=random.choice(myurllist.url[i])opener.open(x).read()print"file downloaded:",x

response_time.append(time.time()-tstart)excepturllib2.URLError,e:error_count.value=error_count.value+1#50 such functions are defined here for 50 clientsprocess=[]deffunc():globalprocess

process.append(multiprocessing.Process(target=send_request3))process.append(multiprocessing.Process(target=send_request4))process.append(multiprocessing.Process(target=send_request5))process.append(multiprocessing.Process(target=send_request6))#append 50 functions hereforiinrange(len(process)):process[i].start()foriinrange(len(process)):process[i].join()print"All work Done..!!"returnstart=float(time.time())func()end=float(time.time())-startprintend

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值