Go to the latest version of this document.
search in docstrings

Search bar offers the following options:

- Term presence. The below example searches for documents that must contain “foo”, might contain “bar” and must not contain “baz”: `+foo bar -baz`
- Wildcards. The below example searches for documents with words beginning with “foo”: `foo*`
- Search in specific fields. The following search matches all objects in "twisted.mail" that matches “search”: `+qname:twisted.mail.* +search`

  Possible fields: 'name', 'qname' (fully qualified name), 'docstring', and 'kind'. Last two fields are only applicable if "search in docstrings" is enabled.

- Fuzzy matches. The following search matches all documents that have a word within 1 edit distance of “foo”: `foo~1`

Results provided by Lunr.js

# ``twisted`.`web`.`iweb`.`IRequest``

interface documentation

`class IRequest(Interface): (source)`

Known implementations: `twisted.web.server.Request`

View In Hierarchy

An HTTP request.

Present Since
9.0

Method | `addCookie` | Set an outgoing HTTP cookie.
Method | `finish` | Indicate that the response to this request is complete.
Method | `getAllHeaders` | Return dictionary mapping the names of all received headers to the last value received for each.
Method | `getClientAddress` | Return the address of the client who submitted this request.
Method | `getClientIP` | Return the IP address of the client who submitted this request.
Method | `getCookie` | Get a cookie that was sent from the network.
Method | `getHeader` | Get an HTTP request header.
Method | `getHost` | Get my originally requesting transport's host.
Method | `getPassword` | Return the HTTP password sent with this request, if any.
Method | `getRequestHostname` | Get the hostname that the HTTP client passed in to the request.
Method | `getRootURL` | Get a previously-remembered URL.
Method | `getSession` | Look up the session associated with this request or create a new one if there is not one.
Method | `getUser` | Return the HTTP user sent with this request, if any.
Method | `isSecure` | Return True if this request is using a secure transport.
Method | `prePathURL` | At any time during resource traversal or resource rendering, returns an absolute URL to the most nested resource which has yet been reached.
Method | `redirect` | Utility function that does a redirect.
Method | `rememberRootURL` | Remember the currently-processed part of the URL for later recalling.
Method | `setETag` | Set an `entity tag` for the outgoing response.
Method | `setHeader` | Set an HTTP response header. Overrides any previously set values for this header.
Method | `setHost` | Change the host and port the request thinks it's using.
Method | `setLastModified` | Set the `Last-Modified` time for the response to this request.
Method | `setResponseCode` | Set the HTTP response code.
Method | `URLPath` | No summary
Method | `write` | Write some data to the body of the response to this request. Response headers are written the first time this method is called, after which new response headers may not be added.
Attribute | `args` | A mapping of decoded query argument names as `bytes` to corresponding query argument values as `list`s of `bytes`. For example, for a URI with `foo=bar&foo=baz&quux=spam` for its query part, `args` will be `{b'foo': [b'bar', b'baz'], b'quux': [b'spam']}`...
Attribute | `content` | A file-like object giving the request body. This may be a file on disk, an `io.BytesIO`, or some other type. The implementation is free to decide on a per-request basis.
Attribute | `method` | A `bytes` giving the HTTP method that was used.
Attribute | `path` | A `bytes` giving the encoded query path of the request URI (not including query arguments).
Attribute | `postpath` | The URL path segments which have not (yet) been processed during resource traversal, as a list of `bytes`.
Attribute | `prepath` | The URL path segments which have been processed during resource traversal, as a list of `bytes`.
Attribute | `requestHeaders` | A `http_headers.Headers` instance giving all received HTTP request headers.
Attribute | `responseHeaders` | A `http_headers.Headers` instance holding all HTTP response headers to be sent.
Attribute | `uri` | A `bytes` giving the full encoded URI which was requested (including query arguments).

def addCookie(k, v, expires=None, domain=None, path=None, max_age=None, comment=None, secure=None): (source)

Set an outgoing HTTP cookie.

In general, you should consider using sessions instead of cookies, see `twisted.web.server.Request.getSession` and the `twisted.web.server.Session` class for details.

def finish(): (source)

Indicate that the response to this request is complete.

def getAllHeaders(): (source)

Return dictionary mapping the names of all received headers to the last value received for each.

Since this method does not return all header information, `requestHeaders.getAllRawHeaders()` may be preferred.

def getClientAddress(): (source)

Return the address of the client who submitted this request.

The address may not be a network address. Callers must check its type before using it.

Returns
an `IAddress` provider. | the client's address.
Present Since
18.4

def getClientIP(): (source)

Return the IP address of the client who submitted this request.

This method is deprecated. See `getClientAddress` instead.

Returns
`str` or `None` | the client IP address or `None` if the request was submitted over a transport where IP addresses do not make sense.

def getCookie(key): (source)

Get a cookie that was sent from the network.

Parameters
key:`bytes` | The name of the cookie to get.
Returns
`bytes` or `None` | The value of the specified cookie, or `None` if that cookie was not present in the request.

def getHeader(key): (source)

Get an HTTP request header.

Parameters
key:`bytes` or `str` | The name of the header to get the value of.
Returns
`bytes` or `str` or `None` | The value of the specified header, or `None` if that header was not present in the request. The string type of the result matches the type of `key`.

def getHost(): (source)

Get my originally requesting transport's host.

Returns
An `IAddress`.

def getPassword(): (source)

Return the HTTP password sent with this request, if any.

If no password was supplied, return the empty string.

Returns
`str` | the HTTP password, if any

def getRequestHostname(): (source)

Get the hostname that the HTTP client passed in to the request.

This will either use the `Host:` header (if it is available; which, for a spec-compliant request, it will be) or the IP address of the host we are listening on if the header is unavailable.

Returns
`bytes` | the requested hostname
Note

This is the host portion of the requested resource, which means that:

1. it might be an IPv4 or IPv6 address, not just a DNS host name,
2. there's no guarantee it's even a valid host name or IP address, since the `Host:` header may be malformed,
3. it does not include the port number.

def getRootURL(): (source)

Get a previously-remembered URL.

Returns
`bytes` | An absolute URL.

def getSession(sessionInterface=None): (source)

Look up the session associated with this request or create a new one if there is not one.

Returns
The `Session` instance identified by the session cookie in the request, or the `sessionInterface` component of that session if `sessionInterface` is specified.

def getUser(): (source)

Return the HTTP user sent with this request, if any.

If no user was supplied, return the empty string.

Returns
`str` | the HTTP user, if any

def isSecure(): (source)

Return True if this request is using a secure transport.

Normally this method returns True if this request's HTTPChannel instance is using a transport that implements ISSLTransport.

This will also return True if setHost() has been called with ssl=True.

Returns
`bool` | True if this request is secure

def prePathURL(): (source)

At any time during resource traversal or resource rendering, returns an absolute URL to the most nested resource which has yet been reached.

Returns
`bytes` | An absolute URL.
See Also
{twisted.web.server.Request.prepath}

def redirect(url): (source)

Utility function that does a redirect.

The request should have finish() called after this.

def rememberRootURL(): (source)

Remember the currently-processed part of the URL for later recalling.

def setETag(etag): (source)

Set an `entity tag` for the outgoing response.

That's "entity tag" as in the HTTP/1.1 ETag header, "used for comparing two or more entities from the same requested resource."

If I am a conditional request, I may modify my response code to `NOT_MODIFIED` or `PRECONDITION_FAILED`, if appropriate for the tag given.

Parameters
etag:`str` | The entity tag for the resource being returned.
Returns
If I am a `If-None-Match` conditional request and the tag matches one in the request, I return `CACHED` to indicate that you should write no body. Otherwise, I return a false value.

def setHeader(k, v): (source)

Set an HTTP response header. Overrides any previously set values for this header.

Parameters
k:`bytes` or `str` | The name of the header for which to set the value.
v:`bytes` or `str` | The value to set for the named header. A `str` will be UTF-8 encoded, which may not interoperable with other implementations. Avoid passing non-ASCII characters if possible.

def setHost(host, port, ssl=0): (source)

Change the host and port the request thinks it's using.

This method is useful for working with reverse HTTP proxies (e.g. both Squid and Apache's mod_proxy can do this), when the address the HTTP client is using is different than the one we're listening on.

For example, Apache may be listening on https://www.example.com, and then forwarding requests to http://localhost:8080, but we don't want HTML produced by Twisted to say 'http://localhost:8080', they should say 'https://www.example.com', so we do:

       request.setHost('www.example.com', 443, ssl=1)

def setLastModified(when): (source)

Set the `Last-Modified` time for the response to this request.

If I am called more than once, I ignore attempts to set Last-Modified earlier, only replacing the Last-Modified time if it is to a later value.

If I am a conditional request, I may modify my response code to `NOT_MODIFIED` if appropriate for the time given.

Parameters
when:`int` or `float` | The last time the resource being returned was modified, in seconds since the epoch.
Returns
If I am a `If-Modified-Since` conditional request and the time given is not newer than the condition, I return `CACHED` to indicate that you should write no body. Otherwise, I return a false value.

def setResponseCode(code, message=None): (source)

Set the HTTP response code.

Parameters
code:`int` | Undocumented
message:`bytes` | Undocumented

def URLPath(): (source)

Returns
A `URLPath` instance which identifies the URL for which this request is.

def write(data): (source)

Write some data to the body of the response to this request. Response headers are written the first time this method is called, after which new response headers may not be added.

Parameters
data:`bytes` | Bytes of the response body.

args = (source)

A mapping of decoded query argument names as `bytes` to corresponding query argument values as `list`s of `bytes`. For example, for a URI with `foo=bar&foo=baz&quux=spam` for its query part, `args` will be `{b'foo': [b'bar', b'baz'], b'quux': [b'spam']}`.

content = (source)

A file-like object giving the request body. This may be a file on disk, an `io.BytesIO`, or some other type. The implementation is free to decide on a per-request basis.

method = (source)

A `bytes` giving the HTTP method that was used.

path = (source)

A `bytes` giving the encoded query path of the request URI (not including query arguments).

postpath = (source)

The URL path segments which have not (yet) been processed during resource traversal, as a list of `bytes`.

prepath = (source)

The URL path segments which have been processed during resource traversal, as a list of `bytes`.

requestHeaders = (source)

A `http_headers.Headers` instance giving all received HTTP request headers.

responseHeaders = (source)

A `http_headers.Headers` instance holding all HTTP response headers to be sent.

uri = (source)

A `bytes` giving the full encoded URI which was requested (including query arguments).