-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver2.py
More file actions
26 lines (24 loc) · 1012 Bytes
/
Copy pathserver2.py
File metadata and controls
26 lines (24 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import socket
host = '127.0.0.1'
port = 5011
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen()
print(f"Server 2 running on {host}:{port}")
while True:
conn, addr = s.accept()
with conn:
# Sending a proper HTTP response
response = 'HTTP/1.1 200 OK\r\n'
response += 'Content-Type: text/html\r\n'
response += 'Content-Length: 19\r\n' # Specify the content length
response += 'Connection: keep-alive\r\n' # Keep the connection alive
response += '\r\n'
response += 'Hello from Server 2' # Body content
try:
conn.sendall(response.encode('utf-8')) # Send response
print(f"Connected by {addr}")
except BrokenPipeError as e:
print(f"Connected by {addr}" + " because HAProxy is doing health checkup.")
except Exception as e:
print(f"Unexpected error: {e}")