Sets the status code for our response. :arg int status_code: Response status code. :arg str reason: Human-readable reason phrase describing the status code (for example, the "Not Found" in ``HTTP/1.1 404 Not Found``). Normally determined automatically from `h
(self, status_code: int, reason: Optional[str] = None)
| 355 | pass |
| 356 | |
| 357 | def set_status(self, status_code: int, reason: Optional[str] = None) -> None: |
| 358 | """Sets the status code for our response. |
| 359 | |
| 360 | :arg int status_code: Response status code. |
| 361 | :arg str reason: Human-readable reason phrase describing the status |
| 362 | code (for example, the "Not Found" in ``HTTP/1.1 404 Not Found``). |
| 363 | Normally determined automatically from `http.client.responses`; this |
| 364 | argument should only be used if you need to use a non-standard |
| 365 | status code. |
| 366 | |
| 367 | .. versionchanged:: 5.0 |
| 368 | |
| 369 | No longer validates that the response code is in |
| 370 | `http.client.responses`. |
| 371 | """ |
| 372 | self._status_code = status_code |
| 373 | if reason is not None: |
| 374 | if "<" in reason or not httputil._ABNF.reason_phrase.fullmatch(reason): |
| 375 | # Logically this would be better as an exception, but this method |
| 376 | # is called on error-handling paths that would need some refactoring |
| 377 | # to tolerate internal errors cleanly. |
| 378 | # |
| 379 | # The check for "<" is a defense-in-depth against XSS attacks (we also |
| 380 | # escape the reason when rendering error pages). |
| 381 | reason = "Unknown" |
| 382 | self._reason = escape.native_str(reason) |
| 383 | else: |
| 384 | self._reason = httputil.responses.get(status_code, "Unknown") |
| 385 | |
| 386 | def get_status(self) -> int: |
| 387 | """Returns the status code for our response.""" |