Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么apachebench在HTTP 1.0会话中通过相同的连接不断重复其请求?_Python_Http_Sockets_Apachebench - Fatal编程技术网

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

Python 为什么apachebench在HTTP 1.0会话中通过相同的连接不断重复其请求?,python,http,sockets,apachebench,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(Socket

我用一种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个会话执行1个请求: ab-n1-c1

当我在响应后不关闭连接,期望客户端在收到我的响应后关闭其端的套接字时,会发生一些奇怪的事情

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
Ab甚至没有尝试等待响应,它在同一个连接中处理了大量请求

为什么??我缺少的是HTTP 1.0中重复使用的某种连接吗?我是否应该始终关闭连接,而不管第一对之后的数据是什么\r\n\r\n


为了重现相同的行为,合适的httperf参数是什么?

值得一提的是,我在ApacheBench/2.3中看到了相同的行为,这不是我所期望的。
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()