为python中的每个HTTP请求创建新的TCP连接 [英] Create new TCP Connections for every HTTP request in python

查看:308
本文介绍了为python中的每个HTTP请求创建新的TCP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的大学项目,我试图开发一个基于python的流量生成器。我在vmware上创建了2个CentOS机器,我使用1作为我的客户端和1作为我的服务器。我使用IP别名技术来增加客户端和服务器数量,只使用单个客户端/服务器机器。到目前为止,我在我的客户端机器上创建了50个IP别名,并在我的服务器机器上创建了10个IP别名。我也使用多处理模块从所有50个客户端到所有10个服务器并发生成流量。我还开发了几个配置文件(1kb,10kb,50kb,100kb,500kb,1mb)在我的服务器上(在/ var / www / html目录,因为我使用Apache服务器),我使用urllib2发送请求到这些配置文件我的客户端机器。这里,当我监视TCP连接数时运行我的脚本,它总是<50。我想增加它说10000。我如何实现这一点?我认为如果为每个新的http请求建立一个新的TCP连接,那么这个目标可以实现。我在正确的道路吗?如果没有亲切指导我正确的路径。

 '''
交通生成器脚本:

这里我使用IP Aliasing在单个vm机器上创建多个客户端。
同样,我在服务器端创建多个服务器。我有大约50个客户端和10个服务器
'''
import multiprocessing
import urllib2
import random
import myurllist所有10台服务器的所有目标网址列表
import time
import socbindtry #script将各种虚拟/别名客户端ips绑定到脚本
response_time = [] #some共享变量
error_count = multiprocessing.Value('i', 0)
def send_request3():#function从别名客户端发送请求ip 1
opener = urllib2.build_opener(socbindtry.BindableHTTPHandler3)#bind到别名客户端ip1
try:
tstart = time.time()
for i in range(myurllist.url):
x = random.choice(myurllist.url [i])
opener.open ()
printfile downloaded:,x
response_time.append(time.time() - tstart)
except urllib2.URLError,e:
error_count.value = error_count .value + 1
def send_request4():#function从别名客户端发送请求ip 2
opener = urllib2.build_opener(socbindtry.BindableHTTPHandler4)#bind到别名客户端ip2
try:
tstart = time.time()
for i in range(myurllist.url):
x = random.choice(myurllist.url [i])
opener.open .read()
printfile downloaded:,x
response_time.append(time.time() - tstart)
except urllib2.URLError,e:
error_count.value = error_count.value + 1
这里为50个客户定义了这样的函数
process = []
def func():
全局进程
process.append (multiprocessing.Process(target = send_request3))
process.append(multiprocessing.Process(target = send_request4))
process.append(multiprocessing.Process(target = send_request5))
进程。 append(multiprocessing.Process(target = send_request6))
#append 50个函数在这里
for i in range(len(process)):
process [i] .start()
for i in range(len(process)):
process [i] .join()
printAll work Done .. !!
return
start = float(time.time())
func()
end = float(time.time()) - start
print end


解决方案

对于这种情况,你可能需要创建一个< a href =https://docs.python.org/2/library/multiprocessing.html#using-a-pool-of-workers =nofollow> pool 工作进程。我不知道10000个进程的池是否可行在你的用例(这是一个非常雄心勃勃的目标),但你应该明确调查这个想法。






的基本思想是你有M个任务要执行,最多N个同时运行。当其中一个工作人员完成任务时,它就可以开始工作,直到所有工作完成为止。 一个主要的优点是,如果一些任务需要很长时间来完成,它们不会阻塞工作的整体进程(只要慢进程的数量是< N)。



下面是你的程序的基本结构。使用 Pool

 从多处理导入池

导入时间
导入随机

def send_request(some_parameter):
print (Do send_request,some_parameter)

time.sleep(random.randint(1,10))#simulate random long process

如果__name__ =='__main__':
pool = Pool(processes = 100)

for i in range(200):
pool.apply_async(send_request,[i])


print(Waiting)
pool.close()
pool.join()
print(Done)
/ pre>

在我的系统上,这个示例程序需要19s(实时)来执行。 在我的Debian系统上,我只能在我达到最大打开文件数之前一次产生一个以上的进程1000次(给定标准 ulimit -n of 1024)。如果你需要这么多的工作线程,你必须以某种方式提高这个限制。即使这样做,如我所说,首先10000并发进程可能是相当有抱负的(至少使用Python)。


For my college project I am trying to develop a python based traffic generator.I have created 2 CentOS machines on vmware and I am using 1 as my client and 1 as my server machine. I have used IP aliasing technique to increase number of clients and severs using just single client/server machine. Upto now I have created 50 IP alias on my client machine and 10 IP alias on my server machine. I am also using multiprocessing module to generate traffic concurrently from all 50 clients to all 10 servers. I have also developed few profiles(1kb,10kb,50kb,100kb,500kb,1mb) on my server(in /var/www/html directory since I am using Apache Server) and I am using urllib2 to send request to these profiles from my client machine. Here while running my scripts when I monitor number of TCP Connections it is always <50. I want to increase it to say 10000. How do I achieve this? I thought that if a new TCP Connection is established for every new http request, then this goal can be achieved. Am I on right path? If not kindly guide to me correct path.

        '''
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

解决方案

For this sort of things, you probably need to create a pool of worker process. I don't know if a pool of 10000 process is viable in your use case (it is a very ambitious goal), but you should definitively investigate that idea.


The basic idea behind a pool is that you have M tasks to perform, with a maximum of N running simultaneously. When one of the worker has finished its task, it is ready to work on an other until all the work is done. One major advantage is that if some number of tasks take long time to complete, they will not block the overall progress of the work (as long as the number of "slow" process is < N).

Along the lines, here would be the basic structure of your program Using Pool:

from multiprocessing import Pool

import time
import random

def send_request(some_parameter):
    print("Do send_request", some_parameter)

    time.sleep(random.randint(1,10)) # simulate randomly long process

if __name__ == '__main__':
    pool = Pool(processes=100)

    for i in range(200):
        pool.apply_async(send_request, [i])


    print("Waiting")
    pool.close()
    pool.join()
    print("Done")

On my system, this sample program took something like 19s (real time) to perform. On my Debian system, I was only able to spawn a little bit more than 1000 processes at a time before I reached the maximum number of open file (given the standard ulimit -n of 1024). You will have to somehow raise that limit if you need such a huge number of working threads. And even if doing so, as I said firstly 10000 concurrent process is probably rather ambitious (at least using Python).

这篇关于为python中的每个HTTP请求创建新的TCP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆