Miscellany
public text v1 · immutable while True:
# Read request lines
req_lines = []
while True:
try:
line = yield self.read_line(delimiter="\r\n")
except tcp.ConnectionOverflowException:
yield self.send_error(400)
self.close()
except (tcp.ConnectionClosedException, socket.error):
return
if not line: break
req_lines.append(line)
# Set up request and response objects
req = HTTPRequest(req_lines)
response = HTTPResponse()
# Request processing will happen here
if req.url == "/":
response.code = 200
response.content = "<html><body>Hello, World!</body></html>"
else:
response.default_error(404)
if req.should_keep_alive():
# It's a persistent connection; send the response and then wait for another request
response.headers["Connection"] = "keep-alive"
yield response.write_out(self.sock)
else:
# Just write and close.
response.headers["Connection"] = "close"
yield response.write_out(self.sock)
self.close()
return