CentOS:连接重置运行python脚本时发生对等错误

2024-10-02 00:38:11 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的大学项目中,我正在使用python构建一个流量生成工具。我在Vmware上开发了自己的linux服务器和客户机。我使用urllib2在python中生成流量。我在这里面临的问题是,当我在客户机上运行脚本时(这些脚本使用多个处理不断地向linux服务器发送请求),它在最初的几分钟内运行良好,比如对于大约2000个请求,但是在那之后,它会显示“connection reset by peer”错误,我的脚本就会崩溃。有什么问题吗?我试着做this,但没用。在

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

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

Tags: toimport脚本clientsendfortimecount
1条回答
网友
1楼 · 发布于 2024-10-02 00:38:11

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

$ netstat -nap > /tmp/pre-stats

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

^{pr2}$

在你完成这项工作之后,这两项检查可能会给你一些想法。例如,服务器端的sockets可能已经用完,有太多进程在CLOSE_WAIT/TIME_WAIT(http://vincent.bernat.im/en/blog/2014-tcp-time-wait-state-linux.html)中等待。这可以很容易地阻止新的套接字打开,从而引发“由对等方重置连接”。在

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

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

相关问题 更多 >

    热门问题