0

对于我的大学项目,我正在使用 python 构建一个流量生成工具。我在 Vmware 上开发了自己的 linux 服务器和客户端。我正在使用 urllib2 在 python 中生成流量。我在这里面临的问题是,当我在客户端机器上运行我的脚本(使用多处理不断向 linux 服务器发送请求)时,它在前几分钟工作正常,比如大约 2000 个请求,但之后它显示“连接重置peer”错误,我的脚本崩溃了。可能是什么问题呢?我试过这样做但没有帮助。

如何防止此超时错误并连续运行脚本数小时?

'''
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=[]
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
4

1 回答 1

0

如果没有更多信息,很难知道具体发生了什么。首先 - 使用 netstat 获取您的基线(在脚本执行之前):

$ netstat -nap > /tmp/pre-stats

然后在收到错误时运行脚本并捕获统计信息:

$ netstats -nap > /tmp/post-stats

在你完成之后,运行检查这两个可能会给你一些想法。因此,例如,您可能在 CLOSE_WAIT/TIME_WAIT (http://vincent.bernat.im/en/blog/2014-tcp-time-wait-state-linux.html)。这很容易阻止新套接字打开,从而引发“对等连接重置”。

你肯定有某种资源问题——你需要做更多的工作来挖掘瓶颈所在的信息。

取决于它是什么 - 可能是您的客户端没有正确关闭 TCP 连接,或者服务器保持连接太久 - 可能有多种情况。IT 也可能是 VMWare 出于某种原因无法跟上 VM 之间的流量。

于 2015-03-23T05:14:18.837 回答