Apache
KeepAlive
code: client.py
import socket
import time
import argparse
def send_http_request(sock, host):
request = f"GET / HTTP/1.1\r\nHost: {host}\r\nConnection: keep-alive\r\n\r\n"
sock.sendall(request.encode('utf-8'))
response = sock.recv(4096)
print("Received:", response.decode('utf-8'))
def test_idle_timeout(host, port, idle_time):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((host, port))
print("Connected to server, sending initial HTTP request...")
send_http_request(sock, host)
print(f"Waiting for {idle_time} seconds...")
time.sleep(idle_time)
print(f"Sending another HTTP request after {idle_time} seconds of idleness...")
try:
send_http_request(sock, host)
except socket.error as e:
print(f"Failed to send or receive data after idle time: {e}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test HTTP keep-alive timeout')
parser.add_argument('host', help='Target host')
parser.add_argument('port', type=int, help='Target port')
parser.add_argument('idle_time', type=int, help='Idle time in seconds')
args = parser.parse_args()
test_idle_timeout(args.host, args.port, args.idle_time)
code: /etc/httpd/conf/httpd.conf
KeepAlive On
KeepAliveTimeout 10
code: client.zsh
ec2-user@ip-172-31-22-88 ~$ python3 client.py 54.216.40.233 80 9
Connected to server, sending initial HTTP request...
Received: HTTP/1.1 200 OK
Date: Sun, 04 May 2025 09:22:59 GMT
Server: Apache/2.4.62 ()
Upgrade: h2,h2c
Connection: Upgrade, Keep-Alive
Last-Modified: Sun, 04 May 2025 08:12:22 GMT
ETag: "13-6344af16cd083"
Accept-Ranges: bytes
Content-Length: 19
Keep-Alive: timeout=10, max=100
Content-Type: text/html; charset=UTF-8
Hello World Apache
Waiting for 9 seconds...
Sending another HTTP request after 9 seconds of idleness...
Received: HTTP/1.1 200 OK
Date: Sun, 04 May 2025 09:23:08 GMT
Server: Apache/2.4.62 ()
Last-Modified: Sun, 04 May 2025 08:12:22 GMT
ETag: "13-6344af16cd083"
Accept-Ranges: bytes
Content-Length: 19
Keep-Alive: timeout=10, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
Hello World Apache
https://scrapbox.io/files/681732ecb18c0fa30bcc5baf.png
code: client.zsh
ec2-user@ip-172-31-22-88 ~$ python3 client.py 54.216.40.233 80 11
Connected to server, sending initial HTTP request...
Received: HTTP/1.1 200 OK
Date: Sun, 04 May 2025 09:27:16 GMT
Server: Apache/2.4.62 ()
Upgrade: h2,h2c
Connection: Upgrade, Keep-Alive
Last-Modified: Sun, 04 May 2025 08:12:22 GMT
ETag: "13-6344af16cd083"
Accept-Ranges: bytes
Content-Length: 19
Keep-Alive: timeout=10, max=100
Content-Type: text/html; charset=UTF-8
Hello World Apache
Waiting for 10 seconds...
Sending another HTTP request after 10 seconds of idleness...
Received:
https://scrapbox.io/files/681733b9efc7f0c44730ef0e.png
code: server.zsh
ec2-user@ip-172-31-24-110 ~$ sysctl net.ipv4.tcp_keepalive_time
net.ipv4.tcp_keepalive_time = 7200
ec2-user@ip-172-31-24-110 ~$ netstat -anto
tcp6 0 0 172.31.24.110:80 34.250.211.17:48680 ESTABLISHED keepalive (7196.61/0/0)
code: server.zsh
ec2-user@ip-172-31-24-110 ~$ sudo sysctl -w net.ipv4.tcp_keepalive_time=300
net.ipv4.tcp_keepalive_time = 300
ec2-user@ip-172-31-24-110 ~$ sudo sysctl -p
ec2-user@ip-172-31-24-110 ~$ netstat -anto
tcp6 0 0 172.31.24.110:80 34.250.211.17:41130 ESTABLISHED keepalive (293.16/0/0)