【发布时间】:2012-03-19 15:21:28
【问题描述】:
我想在我们的一台 Web 服务器上进行一些性能测试,以了解该服务器如何处理大量持久连接。不幸的是,我对 HTTP 和 Web 测试并不十分熟悉。到目前为止,这是我为此获得的 Python 代码:
import http.client
import argparse
import threading
def make_http_connection():
conn = http.client.HTTPConnection(options.server, timeout=30)
conn.connect()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("num", type=int, help="Number of connections to make (integer)")
parser.add_argument("server", type=str, help="Server and port to connect to. Do not prepend \'http://\' for this")
options = parser.parse_args()
for n in range(options.num):
connThread = threading.Thread(target = make_http_connection, args = ())
connThread.daemon = True
connThread.start()
while True:
try:
pass
except KeyboardInterrupt:
break
我的主要问题是:如何让这些连接保持活跃?我设置了一个很长的超时时间,但这是一种非常粗略的方法,我什至不确定它会影响连接。只是每隔一段时间请求一两个字节就可以了吗?
(另外,在不相关的说明中,有没有比我的代码末尾丑陋的while True: 块更好的等待键盘中断的过程?)
【问题讨论】:
-
“持久连接”是什么意思?服务器的正常活动是客户端连接,服务器发送响应并关闭连接。
-
@jjm 特别是使用“Connection: Keep-Alive”标头 (link) 保持活动状态的连接。
标签: python http python-3.x