1. cherrypy
  2. CherryPy


Jason R. Coombs  committed 14b1021

Added a couple of sections to CherryPy basics to introduce basic useage of request, response, and their headers objects. Fixes #1176

  • Participants
  • Parent commits 8001c11
  • Branches cherrypy-3.2.x

Comments (0)

Files changed (1)

File sphinx/source/concepts/basics.rst

View file
 when written using CherryPy::
     import cherrypy
     class HelloWorld:
         def index(self):
             return "Hello world!"
         index.exposed = True
 We assume that you already have :doc:`installed </intro/install>` CherryPy.
    mapping that is automatically generated and encoded by CherryPy; it can
    be used to store session-data in a persistent cookie. For it to work you
    have to enable the session functionality by setting 'tools.session.on' to
-   True in your config. 
+   True in your config.
  * :class:`cherrypy.response <cherrypy._cprequest.Response>` contains the
-   data that is used to build the HTTP response. 
+   data that is used to build the HTTP response.
  * :attr:`cherrypy.response.headers <cherrypy.lib.httputil.HeaderMap>`
    contains a mapping with the header options that will be returned by the
    server, before the contents get sent.
  * :attr:`cherrypy.response.body <cherrypy._cprequest.Response.body>` contains
    the actual contents of the webpage that will be sent as a response.
+CherryPy Response
+The `cherrypy.response` object is available to affect aspects of the response
+to a request. Like the request, the response object is a thread-local,
+meaning although it appears to be a global variable, its value is specific
+to the current thread, and thus the current request.
+One may store arbitrary data in the response object.
+HTTP Headers
+CherryPy exposes the request headers (as sent from the client), and response
+headers (to be returned in the response) in the `headers` attribute of
+`cherrypy.request` and `cherrypy.response`.
+For example, to find out what "host" to which the client intended to connect::
+    @cherrypy.expose
+    def index(self):
+        host = cherrypy.request.headers('Host')
+        return "You have successfully reached " + host
+Or to set headers on the response::
+    @cherrypy.expose
+    def index(self):
+        cherrypy.response.headers['Content-Type'] = 'application/jpeg'
+        return my_jpeg_data()