class MyHandler(http.server.BaseHTTPRequestHandler):
-
- def setup(self):
- http.server.BaseHTTPRequestHandler.setup(self)
- self.protocol_version = "HTTP/1.1" # allow connection: keep-alive
+ protocol_version = 'HTTP/1.1'
def do_HEAD(self):
if self.path == "/redirect1":
self.log_message("to be closed: " + repr(self.close_connection))
def do_GET(self):
- response = b"hello world"
-
"""Respond to a GET request."""
+ response = b"hello world"
if self.path == "/empty":
self.finish()
return
def do_POST(self):
+ """Respond to a POST request."""
response = b"hello post"
- """Respond to a GET request."""
+ content_length = int(self.headers['Content-Length'])
+ body = self.rfile.read(content_length)
if self.path == "/empty":
self.finish()
return
self.send_response(403)
else:
self.send_response(200)
-
- if self.path == "/content-length":
- self.send_header("Content-Length", str(len(response)))
-
if self.path == "/map-simple":
- response = "hello"
+ response = b"hello map"
+
+ self.send_header("Content-Length", str(len(response)))
+ conntype = self.headers.get('Connection', "").lower()
+ if conntype != 'keep-alive':
+ self.close_connection = True
+ else:
+ self.send_header("Connection", "keep-alive")
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(response)
+ self.log_message("to be closed: %d, headers: %s, conn:'%s'" % (self.close_connection, str(self.headers), self.headers.get('Connection', "").lower()))
+ self.log_message("ka:'%s', pv:%s[%s]" % (str(conntype == 'keep-alive'), str(self.protocol_version >= "HTTP/1.1"), self.protocol_version))
class ThreadingSimpleServer(socketserver.ThreadingMixIn,