response.rst (1547B)
1 Response 2 ======== 3 4 Response object. This object is used to control the response that will 5 be sent to the HTTP client. A handler function will take the response 6 object and fill in various parts of the response. For example, a plain 7 text response with the body 'Some example content' could be produced as:: 8 9 def handler(request, response): 10 response.headers.set("Content-Type", "text/plain") 11 response.content = "Some example content" 12 13 The response object also gives access to a ResponseWriter, which 14 allows direct access to the response socket. For example, one could 15 write a similar response but with more explicit control as follows:: 16 17 import time 18 19 def handler(request, response): 20 response.add_required_headers = False # Don't implicitly add HTTP headers 21 response.writer.write_status(200) 22 response.writer.write_header("Content-Type", "text/plain") 23 response.writer.write_header("Content-Length", len("Some example content")) 24 response.writer.end_headers() 25 response.writer.write("Some ") 26 time.sleep(1) 27 response.writer.write("example content") 28 29 Note that when writing the response directly like this it is always 30 necessary to either set the Content-Length header or set 31 `response.close_connection = True`. Without one of these, the client 32 will not be able to determine where the response body ends and will 33 continue to load indefinitely. 34 35 .. _response.Interface: 36 37 :mod:`Interface <wptserve.response>` 38 ------------------------------------ 39 40 .. automodule:: wptserve.response 41 :members: