为什么apachebench会通过相同的连接在HTTP 1.0会话中不断重复其请求?

时间:2012-01-31 12:27:24

标签: python http sockets apachebench

我用一种HTTP服务器创建了一个简单的python脚本:

import SocketServer

response  = b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'
response += b'Content-Type: text/plain\r\nContent-Length: 14\r\n\r\n'
response += b'Hello, world!\n'

class MyTCPHandler(SocketServer.StreamRequestHandler):

    def handle(self):
        while True:
            line = self.rfile.readline().strip()
            print "{} {}:{} wrote: {}".format(self.connection, self.client_address[0], self.client_address[1], line)
            if not line:
                self.wfile.write(response)        
                print 'Sent hello world'
                #break

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

现在我启动ab通过1个conenction做1个请求:     ab -n1 -c1 http://127.0.0.1:9999/

当我在响应后没有关闭连接时,期待客户端在收到响应后关闭套接字,就会发生奇怪的事情。

Ab一直重复通过同一个问题发送相同的请求:

~$ python2.7 ./socket_server.py
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: GET / HTTP/1.0
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: Host: 127.0.0.1:9999
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: User-Agent: ApacheBench/2.3
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: Accept: */*
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote:
Sent hello world
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: GET / HTTP/1.0
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: Host: 127.0.0.1:9999
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: User-Agent: ApacheBench/2.3
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: Accept: */*
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote:
Sent hello world
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: GET / HTTP/1.0
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: Host: 127.0.0.1:9999
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: User-Agent: ApacheBench/2.3
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote: Accept: */*
<socket._socketobject object at 0x1068ca600> 127.0.0.1:64626 wrote:
Sent hello world

......它通过同一个插座重复播放。

我试图从socket中读取原始数据:

import SocketServer

class MyTCPHandler2(SocketServer.BaseRequestHandler):

    def handle(self):
        while True:
            self.data = self.request.recv(1024).strip()
            print "{} wrote:".format(self.client_address[0])
            print self.data

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler2)
    server.serve_forever()

Ab仍然甚至没有尝试等待响应,它在相同的连接中冲动了大量的请求。

为什么呢?是否在HTTP 1.0中重新使用某种连接我不见了?我是否应该始终关闭连接,而不管第一次\ r \ n \ r \ n对之后接下来的数据是什么?

尝试重现相同行为的正确的httperf参数是什么?

0 个答案:

没有答案