MCPcopy Create free account
hub / github.com/Louisym/MiniCC / handle_one_request

Function handle_one_request

tutorials/14b_networking_for_engineers.py:135–154  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

133 response_body = '{"message": "hello from raw socket server!"}'
134
135 def handle_one_request():
136 try:
137 client, addr = server_sock.accept()
138 # 读取请求(简化: 只读一次)
139 request_data = client.recv(1024).decode()
140 first_line = request_data.split("\r\n")[0]
141
142 # 构造 HTTP 响应——就是一段文本
143 response = (
144 "HTTP/1.1 200 OK\r\n"
145 f"Content-Length: {len(response_body)}\r\n"
146 "Content-Type: application/json\r\n"
147 "\r\n"
148 f"{response_body}"
149 )
150 client.sendall(response.encode())
151 client.close()
152 return first_line
153 except socket.timeout:
154 return None
155
156 thread = threading.Thread(target=handle_one_request)
157 thread.start()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected