为Python中的每个http请求创建新的TCP连接

时间:2015-03-27 05:59:53

标签: centos python tcp

对于我的大学项目,我正在尝试开发基于python的流量生成器。我在vmware上创建了2台CentOS机器,我使用1作为我的客户端,1作为我的服务器机器。我使用IP别名技术仅使用单个客户端/服务器机器来增加客户端和服务器的数量。到目前为止,我已在我的客户端计算机上创建了50个IP别名,并在我的服务器计算机上创建了10个IP别名。我还使用多处理模块从所有50个客户端到所有10个服务器同时生成流量。我还在我的服务器上开发了几个配置文件(1kb,10kb,50kb,100kb,500kb,1mb)(因为我使用的是Apache服务器,所以在/ var / www / html目录中)我正在使用urllib2向这些配置文件发送请求我的客户端机器。在我监视TCP连接数时运行脚本时,它总是<50。我想把它增加到10000.我怎么做到这一点?我认为如果为每个新的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
'''
import multiprocessing
import urllib2
import random
import myurllist    #list of all destination urls for all 10 servers
import time
import socbindtry   #script that binds various virtual/aliased client ips to the script
response_time=[]    #some shared variables
error_count=multiprocessing.Value('i',0)
def send_request3():    #function to send requests from alias client ip 1
    opener=urllib2.build_opener(socbindtry.BindableHTTPHandler3)    #bind to alias client ip1
    try:
    tstart=time.time()
    for i in range(myurllist.url):
        x=random.choice(myurllist.url[i])
        opener.open(x).read()
        print "file downloaded:",x
        response_time.append(time.time()-tstart)
    except urllib2.URLError, e:
        error_count.value=error_count.value+1
def send_request4():    #function to send requests from alias client ip 2
    opener=urllib2.build_opener(socbindtry.BindableHTTPHandler4)    #bind to alias client ip2
    try:
    tstart=time.time()
    for i in range(myurllist.url):
        x=random.choice(myurllist.url[i])
        opener.open(x).read()
        print "file downloaded:",x
        response_time.append(time.time()-tstart)
    except urllib2.URLError, e:
        error_count.value=error_count.value+1
#50 such functions are defined here for 50 clients
process=[]
def func():
    global process
    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 here
    for i in range(len(process)):
         process[i].start()
    for i in range(len(process)):
         process[i].join()
    print"All work Done..!!"
         return
start=float(time.time())
func()
end=float(time.time())-start
print end

0 个答案:

没有答案