(self, res)
``Response`` Event Handler

        :param response: the ``Response`` object created when the
            HTTP request was initially received.
        :type response: :class:`~circuits.web.wrappers.Response`

        This handler builds an HTTP response data stream from
        the information contained in the *response* object and
        sends it to the client (firing ``write`` events).
``Response`` Event Handler
Response Event Handler
def _on_response(self, res):
        """``Response`` Event Handler

        :param response: the ``Response`` object created when the
            HTTP request was initially received.
        :type response: :class:`~circuits.web.wrappers.Response`

        This handler builds an HTTP response data stream from
        the information contained in the *response* object and
        sends it to the client (firing ``write`` events).
        """
        # send HTTP response status line and headers

        req = res.request
        headers = res.headers
        sock = req.sock

        if req.method == "HEAD":
            self.fire(write(sock, bytes(res)))
            self.fire(write(sock, bytes(headers)))
        elif res.stream and res.body:
            try:
                data = next(res.body)
            except StopIteration:
                data = None
            self.fire(write(sock, bytes(res)))
            self.fire(write(sock, bytes(headers)))
            self.fire(stream(res, data))
        else:
            self.fire(write(sock, bytes(res)))
            self.fire(write(sock, bytes(headers)))

            if isinstance(res.body, bytes):
                body = res.body
            elif isinstance(res.body, text_type):
                body = res.body.encode(self._encoding)
            else:
                parts = (
                    s
                    if isinstance(s, bytes) else s.encode(self._encoding)
                    for s in res.body if s is not None
                )
                body = b"".join(parts)

            if body:
                if res.chunked:
                    buf = [
                        hex(len(body))[2:].encode(self._encoding),
                        b"\r\n",
                        body,
                        b"\r\n"
                    ]
                    body = b"".join(buf)

                self.fire(write(sock, body))

                if res.chunked:
                    self.fire(write(sock, b"0\r\n\r\n"))

            if not res.stream:
                if res.close:
                    self.fire(close(sock))
                # Delete the request/response objects if present
                if sock in self._clients:
                    del self._clients[sock]
                res.done = True
def _on_response ( self , res ) : # send HTTP response status line and headers req = res . request headers = res . headers sock = req . sock if req . method == "HEAD" : self . fire ( write ( sock , bytes ( res ) ) ) self . fire ( write ( sock , bytes ( headers ) ) ) elif res . stream and res . body : try : data = next ( res . body ) except StopIteration : data = None self . fire ( write ( sock , bytes ( res ) ) ) self . fire ( write ( sock , bytes ( headers ) ) ) self . fire ( stream ( res , data ) ) else : self . fire ( write ( sock , bytes ( res ) ) ) self . fire ( write ( sock , bytes ( headers ) ) ) if isinstance ( res . body , bytes ) : body = res . body elif isinstance ( res . body , text_type ) : body = res . body . encode ( self . _encoding ) else : parts = ( s if isinstance ( s , bytes ) else s . encode ( self . _encoding ) for s in res . body if s is not None ) body = b"" . join ( parts ) if body : if res . chunked : buf = [ hex ( len ( body ) ) [ 2 : ] . encode ( self . _encoding ) , b"\r\n" , body , b"\r\n" ] body = b"" . join ( buf ) self . fire ( write ( sock , body ) ) if res . chunked : self . fire ( write ( sock , b"0\r\n\r\n" ) ) if not res . stream : if res . close : self . fire ( close ( sock ) ) # Delete the request/response objects if present if sock in self . _clients : del self . _clients [ sock ] res . done = True